zhanghuiyan2011 2017-11-23 08:14 采纳率: 0%
浏览 3411

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条回答

  • zhanghuiyan2011 2017-11-23 08:55
    关注

    忘了说报错的信息了,出错的行是:
    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)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改