我有一个带webservice.But的字符串,当我将字符串添加到NSMutableRequest时,方法返回变为null。这是我的代码,
-(NSMutableURLRequest *)createRequest:(NSString *)convertedString{
NSString *temp=[NSString stringWithFormat:@"%@%@",Api_Link,convertedString];
NSLog(@"%@",temp);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:temp]];
[request setHTTPBody:[convertedString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"App IPHONE" forHTTPHeaderField:@"USER_AGENT"];
[request setHTTPMethod:@"POST"];
return(request);
}
答案 0 :(得分:1)
我在Xcode项目中运行了你的代码:
NSString *temp = @"https://www.google.se/";
NSString *temsssp = @"https://www.google.se/";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:temp]];
[request setHTTPBody:[temsssp dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"App IPHONE" forHTTPHeaderField:@"USER_AGENT"];
[request setHTTPMethod:@"POST"];
NSLog(@"%@", request);
<强>结果:强>
2017-02-15 06:43:23.061816 Sneak[5023:3002603] <NSMutableURLRequest: 0x170218120> { URL: https://www.google.se/ }
如果您的有效网址,则您的请求没有任何问题。
如果您需要更多详细信息并提供帮助,请提供有关如何运行此方法的更多信息和代码,我会在您的问题更改或编辑时相应地更新答案。
正如我所提到的,你需要一个有效的URL,我用这行代替你做了NSLog:
NSString *temp = @"somethingsomething";
结果是零。同样,您需要一个有效的网址。