一起学习网 一起学习网

java 请求头Header[] headers对象使用

Header[] headers = {
    new BasicHeader("Content-type", "application/x-www-form-urlencoded")
    ,new BasicHeader("Content-type", "application/x-www-form-urlencoded")
    ,new BasicHeader("Accep", "text/html,text/xml,application/xml")
    ,new BasicHeader("Connection", "keep-alive")
    ,new BasicHeader("keep-alive", "115")
    ,new BasicHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2")
};


String post(String url, Map<String, String> params, Header[] headers) Exception {
    CloseableHttpClient httpclient =  HttpClientManager.(());
    HttpPost httpPost = HttpPost(url);
    (headers != ) {
        httpPost.setHeaders(headers);
    }
    (httpPost);
    List<NameValuePair> nvps = ArrayList<NameValuePair>();
    Set<String> keys = params.keySet();
    (String key : keys) {
        nvps.add(BasicNameValuePair(key, params.get(key)));
    }
    httpPost.setEntity(UrlEncodedFormEntity(nvps, StandardCharsets.));
    CloseableHttpResponse response = httpclient.execute(httpPost);

    {
        HttpEntity entity = response.getEntity();
        EntityUtils.(entity);
    } {
        response.close();
    }
}