加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – apache httpclient内容长度问题

发布时间:2020-12-15 08:39:16 所属栏目:Java 来源:网络整理
导读:我试图使用Apache Httpclient将一些 JSON发布到休息服务.但是,我收到此错误: Exception in thread "main" org.apache.http.ProtocolException: Content-Length header already present UsernamePasswordCredentials defaultcreds = new UsernamePasswordCre
我试图使用Apache Httpclient将一些 JSON发布到休息服务.但是,我收到此错误:

Exception in thread "main" org.apache.http.ProtocolException: Content-Length header already present
UsernamePasswordCredentials defaultcreds = new UsernamePasswordCredentials(USER,PASS);


    HttpHost targetHost = new HttpHost("localhost",8080,"http");

    DefaultHttpClient httpclient = new DefaultHttpClient();

    httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(targetHost.getHostName(),targetHost.getPort()),new UsernamePasswordCredentials(USER,PASS));


    HttpPost httpPost = new HttpPost(urlSuffix) {};

    JSONObject holder = new JSONObject();
    holder.put("name","this is a folder");


    StringEntity se = new StringEntity(holder.toString());

    httpPost.setHeader("Accept","application/json");
    httpPost.setHeader("Content-type","application/json");
    httpPost.setEntity(se);



    HttpResponse resp = httpclient.execute(targetHost,httpPost);

    System.out.println("Resp->" + resp.getStatusLine().getStatusCode());

我读过它因为我已经设置了两次内容长度,但我不确定如何解决它.

解决方法

使用HttClient 4.1.1,您的代码可以正常使用.你使用的是哪个版本?

如果您确定没有在代码中第二次设置Content-Length标头(即上面发布的代码正是您正在运行的代码),那么您可以尝试更新到最新版本的httpclient图书馆.你可能会遇到一个像HTTPCLIENT-795这样模糊不清的错误.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读