从iOS应用程序到Google表单的HTTP POST

我正在处理一个POST请求,并已使用此答案 。 有很多文件NSUrlRequest(和连接),但我很难找出为什么请求不起作用。

  1. 我使用此代码使用HTTP Dev Client执行了成功的POST

    entry.0.single=name&entry.1.single=location&entry.4.single=phoneNumber&entry.2.single=order????&pageNumber=0&backupCache= 
  2. 4个variables(名称,位置,phoneNumber,顺序)都链接到应用程序中的textField。

     - (IBAction)placeOrder:(id)sender { NSURL *nsURL = [[NSURL alloc] initWithString:@"url"]; NSMutableURLRequest *nsMutableURLRequest = [[NSMutableURLRequest alloc] initWithURL:nsURL]; // Set HTTP method to POST [nsMutableURLRequest setHTTPMethod:@"POST"]; // Set up the parameters to send. NSString *paramDataString = [NSString stringWithFormat:@"%@=%@&%@=%@&%@=%@&%@=%@&pageNumber=0&backupCache=",@"entry.0.single", _name, @"entry.1.single", _location, @"entry.4.single", _phoneNumber, @"entry.2.single", _order]; // Encode the parameters to default for NSMutableURLRequest. NSData *paramData = [paramDataString dataUsingEncoding:NSUTF8StringEncoding]; // Set the NSMutableURLRequest body data. [nsMutableURLRequest setHTTPBody: paramData]; // Create NSURLConnection and start the request. NSURLConnection *nsUrlConnection=[[NSURLConnection alloc]initWithRequest:nsMutableURLRequest delegate:self]; [ nsUrlConnection start]; } 

我想我可能会错过一些微妙的东西,但我一直在通过stackoverflow和开发人员文档浇注。 任何想法将不胜感激。 谢谢

你需要实现NSURLConnectionDelegate协议,把[nsUrlConnection setDelegate:self]; 到您的代码中,并将-connectionDidFinishLoading:-connection:didReceiveData:-connectionDidFailWithError:方法添加到代码中,并捕获响应数据:

 .h NSMutableData *responseData; .m - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { responseData = [[NSMutableData alloc] init]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [responseData appendData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"RESPONSE: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]); } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"CONNECTION ERROR: %@", [error localizedDescription]); } 

我使用Swift完成了这个任务。 看看这个回购: https : //github.com/goktugyil/QorumLogs

这里是如何设置它的教程: https : //github.com/goktugyil/QorumLogs/blob/master/Log%20To%20GoogleDocs.md

inheritance人的代码做到这一点:

 private static func sendError(#text: String) { var url = NSURL(string: formURL) var postData = formField1 + "=" + text postData += "&" + formField2 + "=" + "anothertext" postData += "&" + formField3 + "=" + "anothertext" postData += "&" + formField4 + "=" + "anothertext" var request = NSMutableURLRequest(URL: url!) request.HTTPMethod = "POST" request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type") request.HTTPBody = postData.dataUsingEncoding(NSUTF8StringEncoding) var connection = NSURLConnection(request: request, delegate: nil, startImmediately: true) }