Jetty POST请求返回错误代码411

时间:2015-12-14 08:20:37

标签: java jetty jetty-9

这是我运行POST请求的代码

// Instantiate HttpClient
    HttpClient httpClient = new HttpClient();

    // Configure HttpClient, for example:
    httpClient.setFollowRedirects(false);

    // Start HttpClient
    httpClient.start();


    ContentResponse response = httpClient.POST("url")
            .agent("Mozilla/5.0")
            .method(HttpMethod.POST)
            .param("do","login")
            .param("url","")
            .param("vb_login_md5password", this.password)
            .param("vb_login_md5password_utf", this.password)
            .param("s", "")
            .param("vb_login_username", this.username)
            .param("vb_login_password", "")
            .send();
    for(HttpField h : response.getHeaders())
        System.out.println(h.getName() + " = " + h.getValue());

这是输出

Date = Mon, 14 Dec 2015 08:17:08 GMT
Content-Type = text/html
Transfer-Encoding = chunked
Connection = keep-alive
Set-Cookie = __cfduid=dbb11c371945319b1d5943aad06c1977e1450081028; expires=Tue, 13-Dec-16 08:17:08 GMT; path=/; domain=.sythe.org; HttpOnly
Server = cloudflare-nginx
CF-RAY = 2548787ba9dc21b6-EWR

我通过

查看帖子请求的ContentResponse文本
response.getContentAsString()

但由于某种原因,我一直收到411错误,告诉我我的Content-Length为零或未指定。有没有办法设置帖子的内容长度标题?

谢谢!

编辑:这样做对我的帖子请求没有影响

.header(HttpHeader.CONTENT_LENGTH, "0")

1 个答案:

答案 0 :(得分:1)

使用.param("do","login")指定要添加的查询参数。对于实际包含正文的POST,我认为您需要在请求中设置content()。也许FormContentProvider会对你有用吗?