访问 Web 服务时出现 HTTP 错误 408

发布于 2024-10-21 13:02:11 字数 1388 浏览 9 评论 0原文

我正在从 iPhone 向 Web 服务发送 POST 请求,该服务是基于 SOAP 的 REST 层。我在 iPhone 设备和模拟器上进行测试时收到错误 408 请求超时。在使用模拟器进行测试时,我注意到如果我先从浏览器访问网络服务器,然后运行模拟器,问题就会消失并且通信可以正常工作。行为与此问题非常相似 HTTP, 408 Request timeout 这是我用来创建 POST 请求的代码。有没有人遇到过这种行为并找到解决方案?

NSString * urlString = [NSString stringWithFormat:@"%@%@", kBaseURL,method];
NSURL * myURL = [NSURL URLWithString:urlString];
NSLog(@"URL %@", myURL);

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:myURL];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
[theRequest setTimeoutInterval:60]; // leave at default 60 secs 
NSLog(@"Post Body: %@",body);   
NSURLConnection *theConnection = [[NSURLConnection alloc] 
                                  initWithRequest:theRequest 
                                  delegate:self];
if( theConnection ) {
    if (webData){
        [webData release];
    }
    webData = [[NSMutableData data] retain];
    NSLog(@"connection set up");
}
else {
    NSLog(@"theConnection is NULL");
}
self.mainConnection=theConnection;
[theConnection release];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 

I am sending a POST request from iPhone to a webservice, which is a REST layer over SOAP. I am getting Error 408 Request Timeout, testing on both the iPhone device and simulator. When testing with the simulator, I noticed that if I access the webserver from a browser first, then run the simulator, the problem goes away and communication works. Behaviour is very similar to this question HTTP, 408 Request timeout
Here is the code I am using to create the POST request. Has anyone come across this behaviour, and found a solution?

NSString * urlString = [NSString stringWithFormat:@"%@%@", kBaseURL,method];
NSURL * myURL = [NSURL URLWithString:urlString];
NSLog(@"URL %@", myURL);

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:myURL];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
[theRequest setTimeoutInterval:60]; // leave at default 60 secs 
NSLog(@"Post Body: %@",body);   
NSURLConnection *theConnection = [[NSURLConnection alloc] 
                                  initWithRequest:theRequest 
                                  delegate:self];
if( theConnection ) {
    if (webData){
        [webData release];
    }
    webData = [[NSMutableData data] retain];
    NSLog(@"connection set up");
}
else {
    NSLog(@"theConnection is NULL");
}
self.mainConnection=theConnection;
[theConnection release];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蔚蓝源自深海 2024-10-28 13:02:11

对不起我的英语,

我也面临着这个问题。我的应用程序是一个调用 Web 服务的 exe。 Web 服务使用 apache cxf 进行编码。然后,我在客户端收到错误“远程服务器返回意外响应(408)请求超时”。但如果我首先从浏览器访问 wsdl,我的 exe 就会成功运行。我还没有找到任何解决方案。我在调用网络服务之前添加以下代码。

WebClient x = new WebClient();

string source = x.DownloadString("http://......../InfoOperations?wsdl");

  if (source != "")
  {
      // Call your web method here...
  }

它对浏览器做同样的事情。这解决了问题。但通常情况下,我们不需要添加这段代码。

然后,我更改了网络服务编码。我用的是Axis2。但我的问题没有解决。
我认为这个问题与防火墙有关。

连接到 webservice 第一次超时

http://www.mustafakurkcu.com/java/230-cxfclientreturn408.html

sorry for my english,

i faced with this stution, too. My application is an exe that calls a webservice. the webservice is coded with apache cxf. Then, i got an error "The remote server returned an unexpected response (408) Request timeout" in client side. but if i access the wsdl from a browser first, my exe runned successfully. i haven't found any solution. i add the fallowing codes before calls the webservice.

WebClient x = new WebClient();

string source = x.DownloadString("http://......../InfoOperations?wsdl");

  if (source != "")
  {
      // Call your web method here...
  }

It does the same thing with browser. this solved the problem. but normally, we don’t need to add this code.

Then, I changed the webservise coding. I used Axis2. but my problem is not solved.
I thought this problem related to firewall.

Connection to webservice times out first time

http://www.mustafakurkcu.com/java/230-cxfclientreturn408.html

玩心态 2024-10-28 13:02:11

我最终找到了造成这种情况的原因。我尝试更改 URL,发现我可以访问任何其他网站,但无法访问托管我需要通信的 Web 服务的 Web 服务器。我在 3 个不同的网络以及 3G 上的 iPhone 上进行了测试,所以这在我的本地网络上不是问题。我还尝试使用 ASIHTTPRequest 而不是 NSURLConnection,但这没有效果。如果我在同一设备上打开 Web 浏览器并将其指向远程服务器,然后再尝试访问 Web 服务,则连接有效。
事实证明,远程服务器没有正确处理 POST 请求,但它正在处理 GET OK。浏览器使用 GET,因此这就是为什么浏览器能够在我的 Web 服务(使用 POST)无法建立连接时建立连接。一个快速但令人讨厌的解决方法是发送 GET 请求并等待其完成,然后再尝试 POST 请求。

I eventually found what was causing this. I tried changing the URL, and found I could access any other website but not the webserver hosting the webservice I needed to communicate with. I tested on 3 different networks plus iPhone on 3G, so it wasn't a problem on my local network. I also tried using ASIHTTPRequest instead of NSURLConnection, this had no effect. The connection worked if I opened a webbrowser on the same device and pointed it at the remote server before attempting to access the web service.
Turns out the remote server was not handling POST requests correctly, but it was handling GET OK. The browser uses GET, so this is why the browser was able to set up a connection when my web service (using POST) couldn't. A quick and nasty workaround is to send a GET request and wait until it completes before attempting a POST request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文