Java - 使用 HtmlUnit 发送发布请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30687614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:30:21  来源:igfitidea点击:

Java - Sending a post request with HtmlUnit

javahtmlunit

提问by Joe Taylor

Can't really find any help on this but I've been trying to send a post request with HtmlUnit. The code I have is:

在这方面真的找不到任何帮助,但我一直在尝试使用 HtmlUnit 发送发布请求。我的代码是:

final WebClient webClient = new WebClient();

// Instead of requesting the page directly we create a WebRequestSettings object
WebRequest requestSettings = new WebRequest(
  new URL("www.URLHERE.com"), HttpMethod.POST);

// Then we set the request parameters
requestSettings.setRequestParameters(new ArrayList());
requestSettings.getRequestParameters().add(new NameValuePair("name", "value"));
// Finally, we can get the page
HtmlPage page = webClient.getPage(requestSettings);

Is there an easier way I could carry out a POST request?

有没有更简单的方法可以执行 POST 请求?

回答by Arya

This is how it's done

这是怎么做的

public void post() throws Exception
{

    URL url = new URL("YOURURL");
    WebRequest requestSettings = new WebRequest(url, HttpMethod.POST);

    requestSettings.setAdditionalHeader("Accept", "*/*");
    requestSettings.setAdditionalHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    requestSettings.setAdditionalHeader("Referer", "REFURLHERE");
    requestSettings.setAdditionalHeader("Accept-Language", "en-US,en;q=0.8");
    requestSettings.setAdditionalHeader("Accept-Encoding", "gzip,deflate,sdch");
    requestSettings.setAdditionalHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
    requestSettings.setAdditionalHeader("X-Requested-With", "XMLHttpRequest");
    requestSettings.setAdditionalHeader("Cache-Control", "no-cache");
    requestSettings.setAdditionalHeader("Pragma", "no-cache");
    requestSettings.setAdditionalHeader("Origin", "https://YOURHOST");

    requestSettings.setRequestBody("REQUESTBODY");

    Page redirectPage = webClient.getPage(requestSettings);
}

You can customize it however you want. Add/remove headers, add/remove request body, etc ...

您可以根据需要自定义它。添加/删除标头,添加/删除请求正文等...

回答by aviundefined

There are n numbers of possible libraries using which you can call rest web services.

有 n 个可能的库,您可以使用它们来调用其余的 Web 服务。

1) Apache Http client 2) Retrofit from Square 3) Volley from google

1) Apache Http 客户端 2) 改造自 Square 3) 来自谷歌的 Volley

I have used Http Apache client and Retrofit both. Both are awesome.

我使用过 Http Apache 客户端和 Retrofit。两者都很棒。

Here is code example of Apache HTTP client to send Post request

这是 Apache HTTP 客户端发送 Post 请求的代码示例

String token = null;

    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost postRequest = new HttpPost(LOGIN_URL);
    StringBuilder sb = new StringBuilder();
    sb.append("{\"userName\":\"").append(user).append("\",").append("\"password\":\"").append(password).append("\"}");
    String content = sb.toString();
    StringEntity input = new StringEntity(content);
    input.setContentType("application/json");
    postRequest.setHeader("Content-Type", "application/json");
    postRequest.setHeader("Accept", "application/json");

    postRequest.setEntity(input);

    HttpResponse response = httpClient.execute(postRequest);

    if (response.getStatusLine().getStatusCode() != 201)
    {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
    }

    Header[] headers = response.getHeaders("X-Auth-Token");

    if (headers != null && headers.length > 0)
    {
        token = headers[0].getValue();
    }

    return token;