gpt4 book ai didi

iphone - NSURLRequest:如何将 httpMethod "GET"更改为 "POST"

转载 作者:可可西里 更新时间:2023-11-01 03:03:56 24 4
gpt4 key购买 nike

GET 方法,它工作正常。

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


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

如何将 GET 方法转换为 POST 方法?


网址: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);
}

关于iphone - NSURLRequest:如何将 httpMethod "GET"更改为 "POST",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7750452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com