NSURLRequest:如何将httpMethod“GET”更改为“POST”(NSURLRequest: How to change httpMethod “GET” to “POST”)

编程入门 行业动态 更新时间:2024-10-27 13:24:53
NSURLRequest:如何将httpMethod“GET”更改为“POST”(NSURLRequest: How to change httpMethod “GET” to “POST”)

GET方法,它工作正常。

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


我没有POST方法的概念。 请帮帮我。

如何将GET方法变为POST方法?


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

[urlRequest setHTTPMethod:@"POST"];

GET Method, it works fine.

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


I do not have concepts of POST method. Please help me.

How can I chagne GET method to POST method?


url: http://testurl.com/test.html

[urlRequest setHTTPMethod:@"POST"];

最满意答案

尝试这个

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); }

try this

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); }

更多推荐

本文发布于:2023-04-28 14:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1332447.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   NSURLRequest   httpMethod   change   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!