使用从.net服务器(C#与JSON格式)“POST”方法得到响应到Objective-C问题

嗨,我是一个iOS开发人员,

过去2天起,我面临着一个问题,如:在我的.NET开发人员提供了一个API获取响应使用POST方法只有GET

第1步: 在这里我使用我的Objective-C代码

-(void)getUserNameStr: (NSString*)newUsername withPassword: (NSString*)newPassword{ NSString*urlStr = @"http://taxi.expertverification.com/api/v1/ValidUser?"; NSString * myRequestString =[NSString stringWithFormat:@"%@UserName=%@&Password=%@", urlStr, newUsername, newPassword]; NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; [request setHTTPMethod: @"POST"]; //post section [request setHTTPBody: myRequestData]; NSURLSession *session = [NSURLSession sharedSession]; [[session dataTaskWithURL:[NSURL URLWithString:myRequestString] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { // handle response NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSLog(@"requestReply: %@", requestReply); }] resume]; } 

我的回应是:

 requestReply: {"Message":"The requested resource does not support http method 'GET'."} 

第2步:Android代码

 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); Log.d(TAG, "ba1: "+ba1); nameValuePairs.add(new BasicNameValuePair("UserName", "rkb"); nameValuePairs.add(new BasicNameValuePair("Password", "rkb"); try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://taxi.expertverification.com/api/v1/ValidUser?"; httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); String outPut = EntityUtils.toString(entity); Log.i("GET RESPONSE—-", outPut); Log.e("log_tag ******", "good connection"; bitmapOrg.recycle(); }catch (Exception e) { Log.e("log_tag ******", "Error in http connection " + e.toString()); } 

我的回应是:

 true 

意味着我在这里得到适当的回应

在Android中我正在得到响应正确,但在iOS中我不明白为什么?

在这里,我使用了一些其他的第三方SDK也像:AFNetworking,SBJson也但我仍然面临同样的问题,你可以帮我解决这个问题

解决了。 api被滥用。 使用post方法时,数据任务应该初始化url请求。 HTTP正文应该存储发布数据。 顺便说一下,如果服务器不支持https( https://gist.github.com/mlynch/284699d676fe9ed0abfa ),则可能需要在iOS9中禁用应用程序传输安全性。

 -(void)getUserNameStr: (NSString*)newUsername withPassword: (NSString*)newPassword{ NSString*urlStr = @"http://taxi.expertverification.com/api/v1/ValidUser"; NSString * myRequestString =[NSString stringWithFormat:@"UserName=%@&Password=%@", newUsername, newPassword]; NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlStr]]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; [request setHTTPMethod: @"POST"]; //post section [request setHTTPBody: myRequestData]; NSURLSession *session = [NSURLSession sharedSession]; [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { // handle response NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSLog(@"requestReply: %@", requestReply); }] resume]; }