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

时间:2014-10-02 10:23:43

标签: 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] 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,另一种是通过字符串,这只是为了测试,因为我怀疑传递了nsdict对象。还有一种类型的返回是纯文本,我已经在postman(chrome插件)上测试了服务它运行正常。我认为我在收到数据后犯了一些错误。请建议我出路,我会非常感激。

P.S:请求以相同的输出返回postData(参数通过string和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>

0 个答案:

没有答案