linux下使用httpclient的http的put请求,发送json数据一直有错误

linux系统中使用curl工具发送http的put请求可以访问到web服务器的数据,其中curl的命令如下:
curl -v --basic -u admin:public -k http://localhost:8080/api/v2/nodes/emq@127.0.0.1/plugins/emqx_recon -X PUT -i -H "Conten
t-Type:application/json" -d '{"active":"true"}'
使用此命令可以有反馈的json结果,表示已经处理。
而在linux下开发环境eclipse使用httpclient发送http的put请求,数据是json数据,却一直报错,不清楚什么原因,贴上代码:
public String doPutMethod(String host, int port, String URI,String stringJson){
if (closeableHttpClient == null) {
// createCloseableHttpClient();
createCloseableHttpClientWithBasicAuth();
}
String result = "";
HttpPut httpPut = null;
HttpResponse httpResponse = null;
HttpEntity entity = null;
httpPut = new HttpPut("http://" + host + ":" + port + "/" + URI);
//设置header
httpPut.setHeader("Content-Type", "application/json;charset=UTF-8");
httpPut.setHeader("Accept", "application/json");

    StringEntity stringEntity = null;
    stringEntity = new StringEntity(stringJson,ContentType.APPLICATION_JSON);
    stringEntity.setContentEncoding("UTF-8");
    System.out.println(stringEntity.getContentType());
    System.out.println(stringEntity.getContentEncoding());
    try {
        System.out.println(EntityUtils.toString(stringEntity));
    } catch (ParseException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    httpPut.setEntity(stringEntity);

    try {
        httpResponse = closeableHttpClient.execute(httpPut);
        entity = httpResponse.getEntity();
        if (entity != null) {
            result = EntityUtils.toString(entity);
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        httpPut.releaseConnection();
    }
    return result;
}

public static void main(String args[]) {
    String host = "localhost";
    int port = 8080;

    HttpClientWithBasicAuth httpClient = new HttpClientWithBasicAuth();

    //test httpPutMethod
    String URI = "api/v2/nodes/emq@127.0.0.1/plugins/emq_auth_clientid";
    String str = "{\"active\":\"true\"}";

    String result = httpClient.doPutMethod(host, port, URI, str);
    System.out.println(result);

}

1个回答

忘了说报错的信息了,出错的行是:
httpResponse = closeableHttpClient.execute(httpPut);
控制台信息如下:
Content-Type: application/json; charset=UTF-8
Content-Encoding: UTF-8
{"active":"true"}
org.apache.http.NoHttpResponseException: localhost:8080 failed to respond
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:261)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:165)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:167)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:272)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)

at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
at com.dianxin.emq.service.HttpClientWithBasicAuth.doPutMethod(HttpClientWithBasicAuth.java:119)
at com.dianxin.emq.service.HttpClientWithBasicAuth.main(HttpClientWithBasicAuth.java:248)
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!