简体   繁体   中英

Java HttpClient error

When I try to send a POST request with an HttpClient to a website which uses CloudFlare, I don't get the website page content. It looks like I get "blocked" from CloudFlare. How can I get a solution? This is the code I use:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://website/");
// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user", "Bob"));
try {
    httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
} catch (UnsupportedEncodingException e) {
    // writing error to Log
    e.printStackTrace();
}
/*
 * Execute the HTTP Request
 */
try {
    HttpResponse response = httpClient.execute(httpPost);
    HttpEntity respEntity = response.getEntity();

    if (respEntity != null) {
        // EntityUtils to get the response content
        String content =  EntityUtils.toString(respEntity);
    }
} catch (ClientProtocolException e) {
    // writing exception to log
    e.printStackTrace();
} catch (IOException e) {
    // writing exception to log
    e.printStackTrace();
}

检查您的服务器默认情况下( CORS )是否未阻止发布请求

First check and make sure you are sending required parameters in your request and try adding user agent in your request :

params.add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0");

If you really want to check which parameters are being sent in request when you are doing it from browser, one way I know is to use Firefox extension Tamper Data. It will show you all parameters of header and post data and will allow you to modify them also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM