HttpClient执行时出现org.apache.http.client.ClientProtocolException

时间:2014-08-07 12:35:17

标签: java android

我想通过POST请求向服务器发送一些数据。我的代码:

protected Void doInBackground(Void... arg)
        {       
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost post = new HttpPost("https://money.yandex.ru/oauth/authorize");

            post.addHeader("Host", "m.sp-money.yandex.ru");
            post.addHeader("Content-Type", "application/x-www-form-urlencoded");
            post.addHeader("Content-Length", "154");

            //data back from server
            String responseBackFromServer = "";

            try 
            {
                List<NameValuePair> pairs = new ArrayList<NameValuePair>();
                pairs.add(new BasicNameValuePair("client_id", "51"));
                pairs.add(new BasicNameValuePair("response_type", "code"));
                pairs.add(new BasicNameValuePair("redirect_uri", "https://vk.com/"));
                pairs.add(new BasicNameValuePair("scope", "account-info"));

                post.setEntity(new UrlEncodedFormEntity(pairs));
                HttpResponse server_response = httpclient.execute(post);//!!!exception is appearing here!!!

                responseBackFromServer = EntityUtils.toString(server_response.getEntity());

            }
            catch (ClientProtocolException e) 
            {
                e.printStackTrace();
                Log.d(LOG_TAG, "" + e);
            }
}

但是&#34; HttpResponse server_response = httpclient.execute(post);&#34;出现字符串ClientProtocolException。我该如何解决?

1 个答案:

答案 0 :(得分:2)

尝试删除所有addHeader语句。框架应该为您添加它们。

请点击此处查看示例:https://hc.apache.org/httpcomponents-client-4.3.x/quickstart.html