Iphone 如何在ios的httpBody中发布键值对?

Iphone 如何在ios的httpBody中发布键值对?,iphone,web-services,ios7,http-post,parameter-passing,Iphone,Web Services,Ios7,Http Post,Parameter Passing,我必须发送POST请求主体中的键值对才能得到返回,我已经编写了这段代码,但得到了错误的响应 NSString *urlAsString = @"http://someurl.com"; NSDictionary *params = @{@"request" : @"get_pull_down_menu", @"data" : @"0,0,3,1"}; NSString * parameters = [[NSString alloc] initWithF

我必须发送POST请求主体中的键值对才能得到返回,我已经编写了这段代码,但得到了错误的响应

     NSString *urlAsString = @"http://someurl.com";

    NSDictionary *params = @{@"request" : @"get_pull_down_menu", @"data" :   @"0,0,3,1"};

         NSString * parameters = [[NSString alloc] initWithFormat:@"request=get_pull_down_menu&data=0,0,3,1"];



        NSURL *serviceURL = [NSURL URLWithString:urlAsString];

       NSData *postData = [NSKeyedArchiver archivedDataWithRootObject:params];//TO convert nsdict into acceptable nsdata type

       NSData *postData = [parameters dataUsingEncoding:NSUTF8StringEncoding];



       NSMutableURLRequest *requestService = [NSMutableURLRequest requestWithURL:serviceURL];
       [requestService setTimeoutInterval:30.0f];
       [requestService setHTTPMethod:@"POST"];
       [requestService setValue:[NSString stringWithFormat:@"%ld",postData.length] forHTTPHeaderField:@"Content-Length"];
        [requestService setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
       [requestService setHTTPBody:postData ];


    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [NSURLConnection sendAsynchronousRequest:requestService queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){

        if ([data length] >0 && error == nil) {

            NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

            NSLog(@"%ld lendth of recieved data" , html.length);
            NSLog(@"HTML = %@", html);


        }
         else if ([data length] == 0 && error == nil)
        {

            NSLog(@"Nothing was downloaded");


        }
        else if (error != nil)
        {
            NSLog(@"Here is Error Log = %@", error);

        }


    }];
在上面的代码中,您可能会因为看到两种类型的参数而感到困惑,一种是通过nsdict,另一种是通过string,这只是为了测试,因为我怀疑是否传递了nsdict对象。还有一件事,返回的类型是纯文本的,我已经在postman(chrome插件)上测试了这项服务,它工作正常。我想我在收到数据后犯了一些错误。请告诉我出路,我将非常感激

注意:请求返回的输出与postData(参数通过字符串和nsdict发送)相同,但有一些错误。错误日志如下:

2014-10-02 13:38:42.227 [5624:1549]1798 lendth of recieved data
2014-10-02 13:38:42.228 [5624:154907] HTML = <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML><head><script src="http://cdn.spotflux.com/service/partners/"></script><script type="text/javascript" src="http://cdn.spotflux.com/service/launcher/partner.js"></script><TITLE>The page cannot be found</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=Windows-1252">
<STYLE type="text/css">
  BODY { font: 8pt/12pt verdana }
  H1 { font: 13pt/15pt verdana }
  H2 { font: 8pt/12pt verdana }
  A:link { color: red }
  A:visited { color: maroon }
</STYLE>
</HEAD><BODY><TABLE width=500 border=0 cellspacing=10><TR><TD>

<h1>The page cannot be found</h1>
The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.
<hr>
<p>Please try the following:</p>
<ul>
<li>Make sure that the Web site address displayed in the address bar of your browser is spelled and formatted correctly.</li>
<li>If you reached this page by clicking a link, contact
 the Web site administrator to alert them that the link is incorrectly formatted.
</li>
<li>Click the <a href="javascript:history.back(1)">Back</a> button to try another link.</li>
</ul>
<h2>HTTP Error 404 - File or directory not found.<br>Internet Information Services (IIS)</h2>
<hr>
<p>Technical Information (for support personnel)</p>
<ul>
<li>Go to <a href="http://go.microsoft.com/fwlink/?linkid=8180">Microsoft Product Support Services</a> and perform a title search for the words <b>HTTP</b> and <b>404</b>.</li>
<li>Open <b>IIS Help</b>, which is accessible in IIS Manager (inetmgr),
 and search for topics titled <b>Web Site Setup</b>, <b>Common Administrative Tasks</b>, and <b>About Custom Error Messages</b>.</li>
</ul>

</TD></TR></TABLE></BODY></HTML>
2014-10-02 13:38:42.227[5624:1549]1798收到数据的借出量
2014-10-02 13:38:42.228[5624:154907]HTML=
找不到该页
正文{font:8pt/12pt verdana}
H1{font:13pt/15pt verdana}
H2{font:8pt/12pt verdana}
A:链接{颜色:红色}
A:访问{color:maroon}
找不到该页
您正在查找的页面可能已被删除、名称已更改或暂时不可用。

请尝试以下操作:

  • 确保浏览器地址栏中显示的网站地址拼写和格式正确
  • 如果您通过单击链接访问此页面,请与联系 网站管理员提醒他们链接格式不正确。
  • 单击按钮尝试其他链接
HTTP错误404-找不到文件或目录。
Internet信息服务(IIS)
技术信息(针对支持人员)

  • 转到并对词语HTTP和404执行标题搜索
  • 打开IIS帮助,该帮助可在IIS管理器(inetmgr)中访问, 并搜索标题为“网站设置”、“常见管理任务”和“关于自定义错误消息”的主题

将您的内容类型“application/x-www-form-urlencoded charset=utf-8”更改为“application/json”。什么都不会发生,只会减小响应数据的大小。此服务以纯文本形式返回数据。这是服务器端的问题。这里有Post请求&我认为在服务器端他们没有管理它。只需检查服务器端代码。因为我有同样的问题&服务器端。为什么Web服务的测试工具会给出正确的结果?