使用" POST"获得回复从.net服务器(带有json格式的C#)到Objective-c问题的方法

时间:2016-01-11 05:15:48

标签: c# android ios 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,但我仍然面临同样的问题,你可以帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

解决。 api被误用了。使用post方法时,数据任务应该使用url请求初始化。 HTTP正文应该存储帖子数据。顺便说一下,如果服务器不支持https(https://gist.github.com/mlynch/284699d676fe9ed0abfa),您可能需要在iOS9中禁用app transport security。

-(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];
}