首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSURLRequest:如何将httpMethod "GET“改为"POST”

NSURLRequest:如何将httpMethod "GET“改为"POST”
EN

Stack Overflow用户
提问于 2011-10-13 06:54:01
回答 2查看 24.6K关注 0票数 8

找个方法,很好用。

网址:http://myurl.com/test.html?query=id=123&name=kkk

我没有邮政法的概念。请帮帮我。

我怎样才能得到发帖的方法呢?

网址:http://testurl.com/test.html

[urlRequest setHTTPMethod:@"POST"];

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-13 07:06:41

尝尝这个

代码语言:javascript
复制
NSString *post = [NSString stringWithFormat:@"username=%@&password=%@",username.text,password.text];
NSLog(@"%@",post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@" server link here"]]];
[request setHTTPMethod:@"POST"];
NSString *json = @"{}";
NSMutableData *body = [[NSMutableData alloc] init];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
//get response
NSHTTPURLResponse* urlResponse = nil;  
NSError *error = [[NSError alloc] init];  
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);

if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) 
{
    NSLog(@"Response: %@", result);
}
票数 21
EN

Stack Overflow用户

发布于 2011-10-13 06:57:39

代码语言:javascript
复制
// create mutable request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];       
// set GET or POST method
[request setHTTPMethod:@"POST"];
// adding keys with values
NSString *post = @"query=id=123&name=kkk";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
[request addValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7750452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档