Objective-C中的简单http post示例?

Ale*_* Es 51 post cocoa-touch http objective-c ios

我有一个需要登录的php网页(用户ID和密码).我让用户在应用程序中输入信息就好......但我需要一个关于如何对网站发出POST请求的示例.支持网站上的苹果示例相当复杂,显示图片上传..我应该更简单..我只想发布2行文字..任何人都有任何好的例子?

亚历克斯

fre*_*oma 115

这是我最近使用的,它对我来说很好用:

NSString *post = @"key1=val1&key2=val2";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://www.nowhere.com/sendFormHere.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
Run Code Online (Sandbox Code Playgroud)

最初取自http://deusty.blogspot.com/2006/11/sending-http-get-and-post-from-cocoa.html,但该博客似乎不再存在.

  • 5秒的谷歌搜索导致了这个页面...感谢您实际回答它,而不是说"谷歌它".足够的谷歌评论人,geesh (27认同)
  • 那是因为,正如vikingosekundo已经说过的那样,xcode与它无关,它只是你正在使用的IDE.您正在使用Cocoa框架在Objective-C中进行编程. (8认同)
  • @ x3ro,你帮助将谷歌搜索的5秒减少到1秒.谢谢... (2认同)

Nae*_*eem 15

来自Apple的官方网站:

// In body data for the 'application/x-www-form-urlencoded' content type,
// form fields are separated by an ampersand. Note the absence of a
// leading ampersand.
NSString *bodyData = @"name=Jane+Doe&address=123+Main+St";

NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.apple.com"]];

// Set the request's content type to application/x-www-form-urlencoded
[postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

// Designate the request a POST request and specify its body data
[postRequest setHTTPMethod:@"POST"];
[postRequest setHTTPBody:[NSData dataWithBytes:[bodyData UTF8String] length:strlen([bodyData UTF8String])]];

// Initialize the NSURLConnection and proceed as described in
// Retrieving the Contents of a URL
Run Code Online (Sandbox Code Playgroud)

来自:代码与克里斯

    // Create the request.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];

// Specify that it will be a POST request
request.HTTPMethod = @"POST";

// This is how we set header fields
[request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

// Convert your data and set your request's HTTPBody property
NSString *stringData = @"some data";
NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = requestBodyData;

// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Run Code Online (Sandbox Code Playgroud)


Vik*_*ica 12

ASIHTTPRequest使网络通信变得非常简单

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];
[request addFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photos"];
[request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg" forKey:@"photos"];
Run Code Online (Sandbox Code Playgroud)

  • 它已经停产:http://allseeing-i.com/ [request_release]; (8认同)