gpt4 book ai didi

ios - iOS中的HttpRequest Body参数

转载 作者:行者123 更新时间:2023-12-01 17:56:54 45 4
gpt4 key购买 nike

我正在为我的 iOS 应用程序使用网络服务。
我知道如何通过 URL(不是 http Body)发送 http post 请求并使用 NSURLConnection Delegate 获得响应。
但是现在有更好的方法可以用于 Web 服务并在请求正文中传递参数。我在谷歌上找了图书馆,发现 this .
但是代码对我来说有点困难,我想应该有相同的功能,这可以使这更容易一些。
有没有?到目前为止的代码如下。

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
initWithURL:[NSURL
URLWithString:**WebService URL**]];

[request setHTTPMethod:@"POST"];
[request setValue:@"text/json" forHTTPHeaderField:@"Content-type"];

NSString *jsonString = [NSString stringWithFormat:@"{\n\"username\":\"%@\",\n\"%@\":\"%@\",\n\"%@\":\"%@\",\n\"%@\":%@,\n\"%@\":%@,\n\"version\":%@,\n\"name\":\"%@\"\n}",userName, @"password",pass,@"accessToken",token,@"isOnline",@"True",@"accountType",type,@"False",name];

[request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];

[[NSURLConnection alloc] initWithRequest:request delegate:self];
此代码中的 jsonString 是 http 正文。我想在请求正文中传递参数。
现在,在执行此代码后,我在 NSData 变量中得到了 null。而我的 Web 服务函数返回一个值,如果执行成功,它也会返回。
我的代码有什么问题。
谢谢你。

最佳答案

感谢您的回答。最后我找到了完美的解决方案。
我使用了 NSURLConnection 委托(delegate),并且我在 json 中的 HTTPBody 中传递参数。我也在 json 中收到响应。
但是在 httprequestbody 中不允许直接以 json 的形式发送参数,所以我们需要在 NSData 中获取它并设置 content-type。

注意:指定内容类型非常重要。

 -(void)sendDataToServer:(NSString*)userName :(NSString*)name{
{
NSArray *keys = [NSArray arrayWithObjects: @"name",@"accountType",@"isOnline",@"username", nil];
NSArray *objects = [NSArray arrayWithObjects:name,[NSNumber numberWithBool:false],[NSNumber numberWithBool:false],userName, nil];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSString *myJSONString =[jsonDictionary JSONRepresentation];
NSData *myJSONData =[myJSONString dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"myJSONString :%@", myJSONString);
NSLog(@"myJSONData :%@", myJSONData);

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:Your URL string]];
[request setHTTPBody:myJSONData];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];


if (theConnection) {
NSLog(@"connected");
receivedData=[[NSMutableData alloc]init];
} else {

NSLog(@"not connected");
}

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
NSString* responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"response: %@",responseString);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded! Received %lu data",(unsigned long)[receivedData length]);
NSString* responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"response: %@",responseString);


NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableLeaves error:&myError];

// show all values

for(id key in res) {

id value = [res objectForKey:key];

NSString *keyAsString = (NSString *)key;
NSString *valueAsString = (NSString *)value;

NSLog(@"key: %@", keyAsString);
NSLog(@"value: %@", valueAsString);
}
}

P.S:您需要添加 JSON 文件以进行序列化(json.h)。

关于ios - iOS中的HttpRequest Body参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15898979/

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