获取响应状态代码:404,尝试使用NSMutableURLRequest

时间:2015-12-04 09:16:53

标签: ios nsmutableurlrequest

我正在尝试使用NSMutableURLRequest将非常大的JSON字符串上传到服务器。如果postData的长度为239741,则会给出以下回复:

 { status code: 404, headers {
 Connection = "keep-alive";
 "Content-Encoding" = gzip;
 "Content-Type" = "text/html";
 Date = "Fri, 04 Dec 2015 09:12:30 GMT";
 Server = "nginx/1.4.6 (Ubuntu)";
 "Transfer-Encoding" = Identity;
 } }

但如果我尝试将JSON字符串发布到postData长度为206044的服务器,则数据会成功上传到服务器。

如何将无限长度的JSON字符串上传到服务器?

完整的代码如下:

NSString *urlString = [NSString stringWithFormat:@“URL Here”];
NSData *postData = [[NSString stringWithFormat:@"data=%@",postValues] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", (int)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setValue:[NSString stringWithFormat:@"Token %@",AuthToken] forHTTPHeaderField:@"Authorization"];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:50];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSError *error1 = nil;

NSHTTPURLResponse *response=nil;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error1];
NSLog(@"response is %@",response);
NSString* responseString=[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"response string is %@",responseString);
if (responseData) {
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error1];
    return json;
}
else
    NSLog(@"Error : %@",error1.description);

2 个答案:

答案 0 :(得分:2)

首先检查您的网络服务URL

  

404或Not Found错误消息是HTTP标准响应代码   表明客户端能够与给定的通信   服务器,但服务器找不到请求的内容。

您可以使用下面列出的Google ChromeMozila firefox上提供的其他插件来检查您的网络服务。

  1. POSTMAN
  2. REST Client
  3. 这可能会有所帮助。

答案 1 :(得分:1)

查看404的含义。谷歌是你的朋友。这意味着您使用的URL在服务器上不存在。至少这就是服务器所说的。

相关问题