0

我有一个程序调用 Web 服务发布来更新信息,但在 DNS 解析失败的情况下,该应用程序只需等待大约 30 秒,让错误再次出现,然后继续正常执行。

注意:如果 web 服务发布失败,应用程序仍然可以正常运行,所以我不想等待响应。只需发布并继续前进,无论何时出现错误都会回来。

下面是返回错误 410 的发送方法。

// Sends the request via HTTP.
-(void)send {

    // If we don't have a handler, create a default one
if(handler == nil) {
    handler = [[SoapHandler alloc] init];
}

// Make sure the network is available
if([SoapReachability connectedToNetwork] == NO) {
    NSError* error = [NSError errorWithDomain:@"SoapRequest" code:400 userInfo:[NSDictionary dictionaryWithObject:@"The network is not available" forKey:NSLocalizedDescriptionKey]];
    [self handleError: error];
}

// Make sure we can reach the host
if([SoapReachability hostAvailable:url.host] == NO) {
    NSError* error = [NSError errorWithDomain:@"SoapRequest" code:410 userInfo:[NSDictionary dictionaryWithObject:@"The host is not available" forKey:NSLocalizedDescriptionKey]];
    [self handleError: error];
}

// Output the URL if logging is enabled
if(logging) {
    NSLog(@"Loading: %@", url.absoluteString);
}

// Create the request
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: url];
if(soapAction != nil) {
    [request addValue: soapAction forHTTPHeaderField: @"SOAPAction"];
}
if(postData != nil) {
    [request setHTTPMethod: @"POST"];
    [request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField: @"Content-Type"];
    [request setHTTPBody: [postData dataUsingEncoding: NSUTF8StringEncoding]];
    if(self.logging) {
        NSLog(@"%@", postData);
    }
}

// Create the connection
conn = [[NSURLConnection alloc] initWithRequest: request delegate: self];
if(conn) {
    receivedData = [[NSMutableData data] retain];
} else {
    // We will want to call the onerror method selector here...
    if(self.handler != nil) {
        NSError* error = [NSError errorWithDomain:@"SoapRequest" code:404 userInfo: [NSDictionary dictionaryWithObjectsAndKeys: @"Could not create connection", NSLocalizedDescriptionKey,nil]];
        [self handleError: error];
        }
    }
}

任何建议将不胜感激!

提前致谢。:)

4

2 回答 2

1

您可能想要添加一些回报。我相信如果主机无法访问,您不想继续尝试连接。

此外,如果您的一项操作正在阻塞线程,您可能希望在后台队列中运行整个发送方法。

于 2012-10-02T22:11:06.820 回答
0

如果使用 dipatch_queue 线程将不会阻塞。我的代码如下。

    if(connectToHostQueue != NULL)
{
    dispatch_release(connectToHostQueue);
    dispatch_suspend(connectToHostQueue);
    connectToHostQueue = NULL;
}

connectToHostQueue = dispatch_queue_create("hostAvailable",NULL);
dispatch_async(connectToHostQueue, ^{
        isHostAvailable = [SoapReachability hostAvailable:url.host];

    dispatch_async(dispatch_get_main_queue(), ^{

        // don't want to continue if process is canceled
        if(isCancel)
        {
            return;
        }

            // Connection process here..............

......................

             });
});
于 2013-06-06T15:09:03.520 回答