简体   繁体   English

HttpPost 中的 org.apache.http.client.ClientProtocolException

[英]org.apache.http.client.ClientProtocolException in HttpPost

I try upload some string to server.我尝试将一些字符串上传到服务器。 When I try upload on server, in string:当我尝试在服务器上上传时,在字符串中:

        HttpResponse response = httpclient.execute(httppost);

I have error org.apache.http.client.ClientProtocolException.我有错误 org.apache.http.client.ClientProtocolException。 All code:所有代码:

public void sendString(String stringToSend) {

    try {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        HttpPost httppost = new HttpPost(serverAddress);
        InputStreamEntity reqEntity = new InputStreamEntity( new ByteArrayInputStream(stringToSend.getBytes()), stringToSend.length());
        reqEntity.setContentType("application/xml");
        httppost.setEntity(reqEntity);
        HttpResponse response = httpclient.execute(httppost);
        if (response.getStatusLine().getStatusCode() != org.apache.http.HttpStatus.SC_OK) {
            Log.i("SEND", "not send "+response.getStatusLine());
        }else{
            Log.i("SEND", "send ok "+response.getStatusLine());
        }
    } catch (IOException e) {
        Log.w("IOException", e.toString() +" "+ e.getMessage());
    }        
}

This should work这应该工作

public void sendString(String stringToSend) {

try {
    HttpParams httpParams=new BasicHttpParams();
    DefaultHttpClient httpclient = new DefaultHttpClient(httpParams);
    httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    HttpPost httppost = new HttpPost(serverAddress);
    InputStreamEntity reqEntity = new InputStreamEntity( new ByteArrayInputStream(stringToSend.getBytes()), stringToSend.length());
    reqEntity.setContentType("application/xml");
    httppost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httppost);
    if (response.getStatusLine().getStatusCode() != org.apache.http.HttpStatus.SC_OK) {
        Log.i("SEND", "not send "+response.getStatusLine());
    }else{
        Log.i("SEND", "send ok "+response.getStatusLine());
    }
} catch (IOException e) {
    Log.w("IOException", e.toString() +" "+ e.getMessage());
}        

} }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.apache.http.client.ClientProtocolException - org.apache.http.client.ClientProtocolException Apache HttpComponents:org.apache.http.client.ClientProtocolException - Apache HttpComponents: org.apache.http.client.ClientProtocolException HttpClient执行时出现org.apache.http.client.ClientProtocolException - org.apache.http.client.ClientProtocolException appears when HttpClient executes 调用AlchemyAPI时获取org.apache.http.client.ClientProtocolException - Getting org.apache.http.client.ClientProtocolException while calling AlchemyAPI Unirest Java 客户端:kong.unirest.UnirestException:org.apache.http.client.ClientProtocolException - Unirest Java Client : kong.unirest.UnirestException: org.apache.http.client.ClientProtocolException Spring WS-I / O错误:null; 嵌套的异常是org.apache.http.client.ClientProtocolException - Spring WS - I/O error: null; nested exception is org.apache.http.client.ClientProtocolException 使用httpclient 4.1.2连接到https会产生org.apache.http.client.ClientProtocolException - connection to https with httpclient 4.1.2 gives org.apache.http.client.ClientProtocolException java.lang.NoClassDefFoundError:使用jsonparser()时org / apache / http / client / ClientProtocolException - java.lang.NoClassDefFoundError: org/apache/http/client/ClientProtocolException while using the jsonparser() org.apache.http.client.methods.HttpPost和org.apache.commons.httpclient.methods.PostMethod之间的区别? - Difference between org.apache.http.client.methods.HttpPost and org.apache.commons.httpclient.methods.PostMethod? java.lang.ClassNotFoundException:HttpPost 上的 org.apache.http.HttpEntity - java.lang.ClassNotFoundException: org.apache.http.HttpEntity on HttpPost
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM