发布请求适用于浏览器,但不适用于移动应用

时间:2016-07-20 14:01:05

标签: android .net web-services post

我们正在创建Android应用,它有一个登录WebService(http)。

Web服务是用.Net编写的,Web服务的输入和返回形式是json格式。当我们在firefox Rest客户端插件中测试web服务时,输出json文件似乎是正确的,如下所示

{
  "status": true,
  "statusdesc": "success",
  "userid": "cd2725e7-d0bf-4aac-9e5d-feccd0d40405",
  "password": "w1inHr+gPQd+ijxhIWMH/A=="
}

但是当我使用以下代码尝试相同的Web服务时,

try{
            int TIMEOUT_MILLISEC = 30000; // = 30 seconds
            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
            HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);

            SSLContext ctx = SSLContext.getInstance("TLS");
            ctx.init(null, new TrustManager[] { new CustomX509TrustManager() }, new SecureRandom());

            HttpClient httpClient = new DefaultHttpClient(httpParams);
            SSLSocketFactory ssf = new CustomSSLSocketFactory(ctx);
            ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            ClientConnectionManager ccm = httpClient.getConnectionManager();
            SchemeRegistry sr = ccm.getSchemeRegistry();
            sr.register(new Scheme("https", ssf, 443));
            DefaultHttpClient sslClient = new DefaultHttpClient(ccm, httpClient.getParams());

            HttpPost httpPost = new HttpPost(Url);
            MultipartEntity reqEntity = new MultipartEntity();
            reqEntity.addPart("data",new StringBody(obj.toString(), Charset.forName("UTF-8")));
            httpPost.setEntity(reqEntity);

            HttpResponse response = sslClient.execute(httpPost);
            StatusCode = response.getStatusLine().getStatusCode();
            Log.e("Response", "statuscode " + StatusCode);

            HttpEntity entity = response.getEntity();
            Log.e("Response String ",""+entity.toString());
            if (entity != null) {
                responseString = EntityUtils.toString(entity);
            }
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            System.out.println(e);
        }

响应似乎如下

{"userid":null,"password":null,"statusdesc":"Unexpected character encountered while parsing number: Q. Path '', line 1, position 2.\r\n   at Newtonsoft.Json.JsonTextReader.ReadNumberIntoBuffer()\r\n   at Newtonsoft.Json.JsonTextReader.ParseNumber(ReadType readType)\r\n   at Newtonsoft.Json.JsonTextReader.ParseValue()\r\n   at Newtonsoft.Json.JsonTextReader.Read()\r\n   at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)\r\n   at Newtonsoft.Json.Linq.JObject.Parse(String json, JsonLoadSettings settings)\r\n   at CommonWebService.WebServiceCommon.ValidateLogin()","status":false,"emailid":null}

移动代码中的问题是什么

0 个答案:

没有答案