iOS中的HttpRequest Body参数

Dee*_*haV 1 xcode xmlhttprequest ios

我正在为我的iOS应用程序使用Web服务.我知道如何通过URL(而不是http Body)发送http post请求并使用NSURLConnection Delegate获取响应.但是现在我有更好的方法来跟踪Web服务并在请求体中传递参数.我在谷歌上找了库,发现了这个.

但代码对我来说有点困难,我想应该有相同的功能,这可以使这一点更容易.有没有?到目前为止的代码如下.

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];
Run Code Online (Sandbox Code Playgroud)

这段代码中的jsonString是http体.我想在请求体中传递参数.现在,执行此代码后,我在NSData变量中得到null.而我的Web服务函数返回一个值,如果执行成功,它也会返回.我的代码有什么问题.谢谢.

Dee*_*haV 5

谢谢你的回答.最后我找到了完全正常的解决方案.我使用了NSURLConnection委托,我在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);
}  
}
Run Code Online (Sandbox Code Playgroud)

PS:您需要添加用于序列化的JSon文件(json.h).