带参数的IOS HttpPost

时间:2013-04-21 11:39:28

标签: ios cookies http-post httpclient

我想发送带有参数的http帖子,然后从响应中检索cookie。

在android中我使用以下内容:

HttpPost request = new HttpPost("http://stackoverflow.com/");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
nameValuePairs.add(new BasicNameValuePair("parameter", parameter));  
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));  

HttpResponse response = client.execute(request);

List<Cookie> cookies = client.getCookieStore().getCookies();

我尝试在IOS中使用以下内容,但它对我不起作用:

NSURL *authUrl = [[NSURL alloc] initWithString:@"http://stackoverflow.com/"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:authUrl];
[request setHTTPMethod:@"POST"];
NSString * parameters = [NSString stringWithFormat:@"login=%@&password=%@",login,password];
NSData *requestData = [NSData dataWithBytes:[parameters UTF8String] length:[parameters length]];
[request setHTTPBody:requestData];
[request setTimeoutInterval:15.0];
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];

在委托方法中,我收到数据。但数据看起来像是没有参数发送的帖子。它发送链接但不发送参数,我猜。我也尝试设置一个标题,它没有帮助我。

[request addValue:VALUE forHTTPHeaderField:@"login"]; 

使用sendSynchronousRequest也无济于事。

NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

我做错了什么?

1 个答案:

答案 0 :(得分:0)

源正常,但我向错误的网站发送请求。