如何在iOS中的NSMutableURLRequest中添加HTTPBody?
我在请求中通过了HTTPBody。但它无法得到适当的回应 下面是我的代码:如何在iOS中的NSMutableURLRequest中添加HTTPBody?,ios,ios10,nsmutableurlrequest,Ios,Ios10,Nsmutableurlrequest,我在请求中通过了HTTPBody。但它无法得到适当的回应 下面是我的代码: NSURL *url = [NSURL URLWithString:@"https://sample.ios.com/l1/voucher"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"]; [request setValue:@"text/html;
NSURL *url = [NSURL URLWithString:@"https://sample.ios.com/l1/voucher"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"text/html; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
//Now pass your own parameter --------
[request setValue:@"g064a4b5aa95556a7b0b4435d94c317840e9b456" forHTTPHeaderField:@"X-Authorization"];
[request setValue:@"123651236512" forHTTPHeaderField:@"uuid"];
[request setValue:@"myIphone" forHTTPHeaderField:@"devicename"];
NSString *myRequestString =@"voucher=JHAHSDGH-80";
NSLog(@"%@",myRequestString);
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];
[ request setHTTPBody: myRequestData ];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[responseData bytes]];
NSLog(@"content : %@",content);
输出如下所示:
content : {"voucher":["The voucher field is required."]}
那么如何设置HTTPBody呢
请帮帮我 HTTPBody被设置为NSData,这是NSJSONSerialization类的类型 因此,您的解决方案是,根据web服务的需要制作一个NSArray并嵌入到NSDictionary类中 然后使用[NSJSONSerialization dataWithJSONObject:'Your dictionary'选项:NSJSONWritingPrettypted error:&error]将JSON转换为数据,它将返回NSData类obj 然后在请求中添加如下内容:, [请求setHTTPBody:数据]
希望这对你有帮助。如果我答错了您的问题,请告诉我。HTTPBody设置为NSData,这是NSJSONSerialization类的类型 因此,您的解决方案是,根据web服务的需要制作一个NSArray并嵌入到NSDictionary类中 然后使用[NSJSONSerialization dataWithJSONObject:'Your dictionary'选项:NSJSONWritingPrettypted error:&error]将JSON转换为数据,它将返回NSData类obj 然后在请求中添加如下内容:, [请求setHTTPBody:数据]
希望这对你有帮助。如果我弄错了你的问题,请告诉我。你能显示你的URL和参数吗?'devicename,uuid是这个参数吗?我不能显示原始URL,但它在邮递员上工作,但我不能getting@KKRocks:Yes devicename、uuid、X-Authorization是3个参数,而凭证=JHAHSDGH-80是参数或标题?能否显示您的URL和参数,'devicename,uuid是这个参数吗?我不能显示原始URL,但它在邮递员上工作,但我不能getting@KKRocks:Yes devicename、uuid、X-Authorization是3个参数,而凭证=JHAHSDGH-80是参数或标题?@SRNayak:您的HTTPBody需要提供什么?NSArray*name=@[a,b,c]NSDictionary*dictionary=[[NSDictionary alloc]initWithObjects:@[name、@“hello”、]forKeys:@[@“firstName”、@“message”];NSData*data=[NSJSONSerialization dataWithJSONObject:dictionary选项:NSJSONWritingPretyPrinted error:nil][requestToPostDataField setHTTPBody:data];尝试此方法。我的正文将是凭证=GSJWBJEB-90NSDictionary*dictionary=[[NSDictionary alloc]initWithObject:@[]forKeys:@[@“firstName”,@“message”]];NSData*数据=[NSJSONSerialization dataWithJSONObject:字典选项:NSJSONWriting预打印错误:nil][requestToPostDataField setHTTPBody:数据]NSDictionary*字典=[NSDictionary alloc]initWithObject:@[@“凭证”]forKey:@[@“GSJWBJEB-9”];NSData*数据=[NSJSONSerialization dataWithJSONObject:dictionary选项:NSJSONWriting预打印错误:nil][requestToPostDataField setHTTPBody:data]尝试以下操作one@SRNayak:您的HTTPBody需要馈送什么?NSArray*名称=@[a,b,c]NSDictionary*字典=[[NSDictionary alloc]initWithObjects:@[name,@“hello”,]forKeys:@[@“firstName”,@“message”]];NSData*data=[NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettypted error:nil][requestToPostDataField setHTTPBody:data];尝试此方法。我的正文将是凭证=GSJWBJEB-90NSDictionary*dictionary=[NSDictionary alloc]initWithObject:@[]forKeys:@[@“名字”,“消息”];NSData*data=[NSJSONSerialization dataWithJSONObject:dictionary选项:NSJSONWriting预打印错误:nil][requestToPostDataField setHTTPBody:data]NSDictionary*字典=[[NSDictionary alloc]initWithObject:@[@“凭证”]forKey:@[@“GSJWBJEB-9”];NSData*数据=[NSJSONSerialization dataWithJSONObject:dictionary选项:NSJSONWritingPrettypted错误:nil][requestToPostDataField setHTTPBody:data]尝试此选项