0
我試圖提交用戶輸入到網頁,然後檢索返回的數據。這個網頁取一個人的名字,並從數據庫中返回他/她的信息。當用戶點擊一個按鈕,然後將用戶輸入發送到該網頁時,我試圖發出POST請求。但是當我打印出返回的數據時,它只是該網頁的html源代碼,只有沒有javaScript。我如何更改/添加下面的函數來獲取人員的信息頁面?我花了很多時間,但仍然無法弄清楚。請幫幫我!謝謝!NSURLSession POST請求返回HTML網頁代碼
-(void) sendHTTPPost{
NSString* input = nameTextField.text;
//1
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://find.pitt.edu"]];
[req setHTTPMethod:@"POST"];
//2
NSData* postData = [input dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//3
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
[req setValue:postLength forHTTPHeaderField:@"Content-Length"];
[req setValue:@"text/html" forHTTPHeaderField:@"Content-type"];
[req setHTTPBody:postData];
//4
NSURLSession *session = [NSURLSession sharedSession];
//5
NSURLSessionDataTask *task = [session dataTaskWithRequest:req
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// Do something with response data here - convert to JSON, check if error exists, etc....
if(error != nil){
NSLog(@"error: %@", error);
}
else{
NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", str);
NSLog(@"response = %@", response);
}
}];
[task resume];
}