简体   繁体   中英

org.apache.http.client.ClientProtocolException appears when HttpClient executes

I want to send to server some data via POST-request. My code:

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);
            }
}

But on "HttpResponse server_response = httpclient.execute(post);" string ClientProtocolException appears. How can I fix it?

Try removing all the addHeader statements. The framework should add them for you.

See here for an example: https://hc.apache.org/httpcomponents-client-4.3.x/quickstart.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM