从Android消费WCF服务时出现401错误

时间:2014-10-07 04:09:45

标签: c# android wcf

我必须从Android使用某个WCF服务。我发送POST请求并发布某些参数。在这样做的同时,我收到了401 Unauthorized Error。如果我将相同的值发布到POSTMAN(Google Chrome扩展程序)中的相同网址,则会完美地发布请求。当我尝试执行GET请求时会发生类似情况。

这是我的POST代码: -

HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(Constants.AddStatusEndpoint);

            try 
            {
                // Set header with AccessToken
                httppost.setHeader("Authorization", accessToken);

                //Add parameters as Form encoded
                //
                /*
                */

                List <NameValuePair> nvps = new ArrayList <NameValuePair>(5);
                   nvps.add(new BasicNameValuePair("name",username));
                nvps.add(new BasicNameValuePair("comment", comment));
                nvps.add(new BasicNameValuePair("status", status));
                nvps.add(new BasicNameValuePair("sTime", startTime));
                nvps.add(new BasicNameValuePair("location",""));


                AbstractHttpEntity entity;
                entity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);

                entity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
                entity.setContentEncoding("UTF-8");
                httppost.setEntity(entity);

                //Fetch the response
                HttpResponse responsenext = httpclient.execute(httppost);

                HttpEntity entitynext = responsenext.getEntity();
                AddedResult= EntityUtils.toString(entitynext);

                JSONArray jObject;
                try 
                {
                    jObject = new JSONArray(AddedResult);
                    for (int i = 0; i < jObject.length(); i++)
                    {
                        JSONObject menuObject = jObject.getJSONObject(i);

                        String name= menuObject.getString("name");
                        String status= menuObject.getString("status");
                        String starttime= menuObject.getString("sTime");

                        resultList.add(new FinalResponse(name, status, starttime, starttime, starttime, "true"));



                    }

                }
                catch (JSONException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

0 个答案:

没有答案