xcode 使用 NSURLConnection POST - 没有 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8979317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:04:37  来源:igfitidea点击:

POST with NSURLConnection - NO JSON

objective-cxcodepostnsurlconnection

提问by whitebreadb

I am trying to write an iPhone app in Objective-C. I need to POST data using NSURLConnection. Every example I can find deals with JSON; I do not need to use JSON. All I need to do is POST the data and get a simple 1 or 0 (succeed or fail) from a PHP script. Nothing more.

我正在尝试用 Objective-C 编写一个 iPhone 应用程序。我需要使用 NSURLConnection POST 数据。我能找到的每个例子都与 JSON 相关;我不需要使用 JSON。我需要做的就是发布数据并从 PHP 脚本中获取一个简单的 1 或 0(成功或失败)。而已。

I came across this code but I am not sure how to use it or modify it to not use JSON:

我遇到了这段代码,但我不确定如何使用它或修改它以不使用 JSON:

- (void)performRequest {
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL                   URLWithString:@"http://someplace.com/"]];
    [request setValue:@"Some Value" forHTTPHeaderField:@"Some-Header"];
    [request setHTTPBody:@"{\"add_json\":\"here\"}"];
    [request setHTTPMethod:@"POST"];
    [NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
 // Fail..
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
 // Request performed.
}

回答by Nick Lockwood

Here's how to create an ordinary post.

这是创建普通帖子的方法。

First create a request of the right type:

首先创建一个正确类型的请求:

NSURL *URL = [NSURL URLWithString:@"http://example.com/somepath"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
request.HTTPMethod = @"POST";

Now format your post data as a URL-encoded string, like this:

现在将您的帖子数据格式化为 URL 编码的字符串,如下所示:

NSString *params = @"param1=value1&param2=value2&etc...";

Remember to encode the individual parameters using percent encoding. You can't entirely rely on the NSString stringByAddingPercentEscapesUsingEncoding method for this (google to find out why) but it's a good start.

请记住使用百分比编码对各个参数进行编码。你不能完全依赖 NSString stringByAddingPercentEscapesUsingEncoding 方法(谷歌找出原因),但这是一个好的开始。

Now we add the post data to your request:

现在我们将发布数据添加到您的请求中:

NSData *data = [params dataUsingEncoding:NSUTF8StringEncoding];
[request addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"%i", [data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];

And that's it, now just send your request as normal using NSURLConnection (or whatever).

就是这样,现在只需使用 NSURLConnection (或其他方式)像往常一样发送您的请求。

To interpret the response that comes back, see Maudicus's answer.

要解释返回的响应,请参阅 Maudicus 的回答。

回答by Jesse Black

You can use the following NSURLConnection method if you target ios 2.0 - 4.3 (It seems to be deprecated in ios 5)

如果您的目标是 ios 2.0 - 4.3,则可以使用以下 NSURLConnection 方法(在 ios 5 中似乎已弃用)

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
  NSString * string = [[NSString alloc] initWithData:data encoding:
                       NSASCIIStringEncoding];

  if (string.intValue == 1) {

  } else {

  }
}

回答by Todd

I've a very similar situation to whitebreadb. I'm not disagreeing with the answers submitted and accepted but would like to post my own as the code provided here didn't work for me (my PHP script reported the submitted parameter as a zero-length string) but I did find this questionthat helped.

我的情况与 whitebreadb 非常相似。我并不反对提交和接受的答案,但想发布我自己的答案,因为此处提供的代码对我不起作用(我的 PHP 脚本将提交的参数报告为零长度字符串),但我确实找到了这个问题这有帮助。

I used this to perform a posting to my PHP script:

我用它来发布到我的 PHP 脚本:

NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.myphpscriptlocation.net/index.php?userID=%@",self.userID_field.stringValue]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
request.HTTPMethod = @"POST";
NSURLConnection *c = [NSURLConnection connectionWithRequest:request delegate:self];