1
我想用參數發送http post,然後從響應中檢索cookie。帶參數的IOS HttpPost
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];
我做錯了什麼?
根據登錄名和密碼參數是什麼,這條線不能正常工作:'NSData * requestData = [NSData dataWithBytes:[parameters UTF8String] length:[parameters length]];'因爲計算的長度可能是錯誤的。錯誤是否從服務器獲得驗證錯誤? – Wain 2013-04-21 12:51:26
一切正常(長度是正確的)。服務器沒有驗證錯誤。但它沒有正確發送參數。我得到的網頁內容與我沒有參數的url相同。 – inc 2013-04-21 13:34:01
你是如何證明一切正常的?您是否驗證過實際發送的請求數據(使用代理檢測到)還是檢查了服務器收到的數據? – Wain 2013-04-21 13:46:16