获取使用与QUOT响应; POST"从.NET服务器(C#与JSON格式),以Objective-C的问题的方法 [英] Getting response using "POST" method from .net server(C# with json format) to Objective-c issue

查看:152
本文介绍了获取使用与QUOT响应; POST"从.NET服务器(C#与JSON格式),以Objective-C的问题的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是一名iOS开发,

Hi I am a iOS Developer,

最近两天以后我面临的一个问题,如:在我的.NET开发人员提供了我一个API使用得到响应的 POST方法只不是获取

Past 2 days onwards i am facing one issue like: In my .net developer provided me one API for getting response using POST method only not GET

第1步:
   在这里,我用我的Objective-C code

-(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的code

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

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

means i am getting proper response here

在Android的我得到响应currectly但iOS中我没有得到为什么?

In Android i am getting response currectly but in iOS i am not getting Why?

在这里,我使用了一些其他第三方SDK的还喜欢:你可以PLZ AFNetworking,SBJson也不过还是我面临同样的问题,帮助我对这个问题

Here i used some other 3rd party SDK's also like : AFNetworking, SBJson also but still i am facing same issue can you plz help me out regarding this issue

推荐答案

解决。 API是滥用。数据任务应该使用POST方法时URL请求初始化。 HTTP主体应存储后的数据。顺便说一句,您可能需要禁用iOS9应用传输安全性,如果服务器不支持HTTPS(的https: //gist.github.com/mlynch/284699d676fe9ed0abfa )。

Solved. The api is misused. The data task should init with url request when using post method. HTTP body should store post data. By the way, you may need to disable app transport security in iOS9, if the server do not support https (https://gist.github.com/mlynch/284699d676fe9ed0abfa).

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

这篇关于获取使用与QUOT响应; POST&QUOT;从.NET服务器(C#与JSON格式),以Objective-C的问题的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆