【问题标题】:Issue in json request response, getting blank responsejson请求响应中的问题,得到空白响应
【发布时间】:2013-05-17 11:13:54
【问题描述】:

我在 iOS 的简单请求响应中遇到了一个问题,我在请求带有一个 post 参数的 url 时得到空白响应,其中 url 因为它在 android 和 webbrowser 中完美运行

朋友详细的,我要打电话

http://example.com/GetCountries

使用以下 http 帖子参数

"key"="Abcd1234"

它以前可以工作,但是从最近几天开始它就不能工作了,如果我检查 NSError 它显示我网络连接丢失。

这里还有一件值得注意的事情是相同的服务器代码在不同的 url 上,它工作正常,您可以按如下方式测试该 url

http://example.com/GetCountries

使用以下 http 帖子参数

"key"="Abcd1234"

这是用于测试 ios 源代码的 Dropbox 链接,该文件夹还包含 Web services test.htm 文件,用于测试具有相同 post 参数的相同 url 在浏览器中工作但在 ios 设备中工作。

测试代码: https://dl.dropboxusercontent.com/s/lqrl5b95j2s54mm/Testing.zip?token_hash=AAFgoNfUpQ4FkeswnPdGiMVzdMtSM6js9KySJm_OH6lZXQ&dl=1

谢谢

【问题讨论】:

  • 编写一个演示问题的小演示应用程序,然后将其上传到 DropBox。更新问题,然后人们会查看它。
  • @DavidH 你是对的,我应该在问这里之前上传它,但目前我不能 bcz 我没有我的 mac,如果可能的话,请用你的任何项目管理它,只需更改 url任何工作项目,再次抱歉
  • 它在网络浏览器中什么也不做。如果您可以制作一个有效的 curl 命令,那将有所帮助。
  • 我已经更新了问题并添加了示例源代码的 Dropbox url。谢谢
  • Parker,如果可能的话,您可以转储 NSURLRequest - 正文和标头。虽然使用这个库可以让您继续,但它并没有回答为什么看起来格式正确的表单不起作用的问题。您可以从我之前发布的代码中获取日志行。或者发布您修改后的方法现在的样子,我会这样做。

标签: ios web-services request browser response


【解决方案1】:

所以我无法让表单本身工作,但能够重新制作它来工作。注意几点:

  • 您应该转换为 ARC!
  • 您需要对连接的强引用,以便稍后释放它(而不是在委托方法中!)
  • 您需要委托 connectionSucceeded 方法(以记录响应!)

代码:

- (void)asynchronousRequest
{
    [activity startAnimating];

    NSString *requesturl = lblURL.text;
    NSLog(@"requesturl=%@", requesturl);
    NSURL *theURL = [NSURL URLWithString:requesturl];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"content-type"];
    [request setURL:theURL];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setTimeoutInterval:60.0];
    [request setHTTPMethod:@"POST"];

    NSString *str = [NSString stringWithFormat:@"key=%@", [self URLencodedString:@"Abcd1234"]];
    NSLog(@"BODY: %@", str);
    NSData *body = [str dataUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"URL : %@", requesturl);
    NSLog(@"REQ : %@", request);

    [request setHTTPBody:body];
    [request addValue:[NSString stringWithFormat:@"%u", [body length]] forHTTPHeaderField:@"Content-Length"];


    NSLog(@"AllFields : %@", [request allHTTPHeaderFields]);
    NSLog(@"HTTPBody : %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);
    NSLog(@"HTTPMethod : %@", [request HTTPMethod]);

    self.activeDownload = [NSMutableData data];

    conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    assert(conn);
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    assert([response isKindOfClass:[NSHTTPURLResponse class]]);
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 
    NSLog(@"GOT %d", [httpResponse statusCode]);

}
- (NSString *)URLencodedString:(NSString *)s
{
    CFStringRef str = CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)s,  NULL,  (CFStringRef)@"!*'();:@&;=+$,/?%#[]",  kCFStringEncodingUTF8);
    NSString *newString = [(NSString *)str stringByReplacingOccurrencesOfString:@" " withString:@"+"];
    if(str) CFRelease(str);
    return newString;
}

编辑:修改后的代码仍然无效:

- (void)asynchronousRequest
{

    [activity startAnimating];

    NSString *boundary = @"1010101010"; // DFH no need for the leading '--'
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];


    NSMutableDictionary *postVariables = [[NSMutableDictionary alloc] init];

    [postVariables setValue:@"Abcd1234" forKey:@"key"];


    NSString *requesturl = lblURL.text;

    NSMutableString *myStr = [[NSMutableString alloc] init];

    NSString *str;

    // DFH - strategy is to have each line append its own terminating newline/return
    str = [NSString stringWithFormat:@"--%@\r\n",boundary]; // DFH initial boundary
    [myStr appendString:str];


    NSArray *formKeys = [postVariables allKeys];
    for (int i = 0; i < [formKeys count]; i++) {
        str = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n%@\r\n",[formKeys objectAtIndex:i],[postVariables valueForKey:[formKeys objectAtIndex:i]]];
        [myStr appendString:str];

        str = [NSString stringWithFormat:@"--%@\r\n",boundary]; // DFH mid or terminating boundary
        [myStr appendString:str];
    }
    NSLog(@"BODY: %@", myStr);
    NSData *body = [myStr dataUsingEncoding:NSUTF8StringEncoding];

    requesturl = [self encodeStringForURL:requesturl];
    NSLog(@"requesturl=%@", requesturl);

    NSURL *theURL = [NSURL URLWithString:requesturl];

    self.activeDownload = [NSMutableData data];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:theURL];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setTimeoutInterval:60.0];
    [request setHTTPMethod:@"POST"];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"]; // DFH you add addValue, I always use setValue

    NSLog(@"URL : %@", requesturl);
    NSLog(@"REQ : %@", request);
    NSLog(@"ContentType \"%@\"", contentType);

    if(body)
    {
        [request setHTTPBody:body];
    }
    NSLog(@"AllFields : %@", [request allHTTPHeaderFields]);
    NSLog(@"HTTPBody : %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);
    NSLog(@"HTTPMethod : %@", [request HTTPMethod]);

    conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    assert(conn);
}

【讨论】:

  • 谢谢你这是一个有用的代码并且工作正常,但是在我将图像发布到服务器的其他 Web 服务中,我必须使用 multipart/form-data,并且再次出现空白响应的相同问题。我觉得整个问题只是因为内容类型多部分/表单数据。如果您可以使用 multipart/form-data 检查相同的帖子内容,那将不胜感激。
  • 我花了一个小时或更长时间试图让它与您拥有的原始代码一起工作。您的原始代码没有正确格式化表单,但即使如此,除了您观察到的断开连接外,我什么也得不到。我对代码有点不确定,因为我自己不使用它,但我会发布我所做的。如果你可以使用“curl”让它工作,那会有所帮助。 SO上有一些示例帖子显示使用多部分表单,我也查看了标准 - 没有帮助。
  • 我只是在我所做的更改周围附加了带有“// DFH”符号的修改后的代码。看:w3.org/TR/html401/interact/forms.html#h-17.13.4chxo.com/be2/20050724_93bf.html。也是包含边界的行,第二个链接说使用逗号而不是';'。
【解决方案2】:

我使用了 ASIHTTP 库,我的问题已经解决了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 2014-01-13
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多