如何将头字段和参数添加到GET请求?

时间:2015-09-21 17:34:24

标签: ios objective-c iphone json httpresponse

我想从服务器发回JSON请求以获取JSON响应。应在标题中添加三个参数,并将一个参数作为查询字符串。现在我发送这样的请求 -

  NSString *strUrl = [NSString stringWithFormat:@"https://www.ngyripq.com/query?number=123456789"];
  NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:strUrl] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];

  [request setHTTPMethod:@"GET"];
  [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  [request addValue:@"my parameter 1" forHTTPHeaderField:@"parameter 1"];
  [request addValue:@"my parameter 2" forHTTPHeaderField:@"parameter 2"];

  DLog(@"%@", [request allHTTPHeaderFields])
  NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

我在didReceiveResponse中收到错误响应,状态代码为:404。我在这里有什么遗漏..?在此先感谢您的帮助。

   <NSHTTPURLResponse: 0x7fc28e7cddc0> { URL: <NSHTTPURLResponse: 0x7fc28e7cddc0> { URL: https://www.ngyripq.com/query?number=123456789 } { status code: 404, headers {
    Connection = close;
   "Content-Encoding" = gzip;
   "Content-Length" = 205;
   "Content-Type" = "text/html; charset=UTF-8";
   Date = "Mon, 21 Sep 2015 17:05:27 GMT";
   Server = "Apache/2.2.3 (Red Hat)";
   Vary = "Accept-Encoding,User-Agent";
  "X-Powered-By" = "Servlet/2.5 JSP/2.1";

}}

1 个答案:

答案 0 :(得分:1)

HTTP错误404表示如果开发人员因其他原因未抛出此错误,则找不到页面。

要做的第一件事就是检查你的网址是否正常。然后尝试使用POSTMAN(https://www.getpostman.com)来发出请求以查看它是否正常工作。

然后在此网址中创建请求文件夹:http://requestb.in

并将您的请求指向它,然后您可以查看您的标头是否正常。