请求头 二

get:

@Test
public void getParams() {

    // 获取连接客户端工具
    CloseableHttpClient httpClient = HttpClients.createDefault();

    String entityStr = null;
    CloseableHttpResponse response = null;

    try {
        /*
         * 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
         */
        URIBuilder uriBuilder = new URIBuilder("http://www.baidu.com");
        /** 第一种添加参数的形式 */
        /*uriBuilder.addParameter("name", "root");
        uriBuilder.addParameter("password", "123456");*/
        /** 第二种添加参数的形式 */
        List<NameValuePair> list = new LinkedList<>();
        BasicNameValuePair param1 = new BasicNameValuePair("name", "root");
        BasicNameValuePair param2 = new BasicNameValuePair("password", "123456");
        list.add(param1);
        list.add(param2);
        uriBuilder.setParameters(list);

        // 根据带参数的URI对象构建GET请求对象
        HttpGet httpGet = new HttpGet(uriBuilder.build());

        /* 
         * 添加请求头信息
         */
        // 浏览器表示
        httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
        // 传输的类型
        httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");

        // 执行请求
        response = httpClient.execute(httpGet);
        // 获得响应的实体对象
        HttpEntity entity = response.getEntity();
        // 使用Apache提供的工具类进行转换成字符串
        entityStr = EntityUtils.toString(entity, "UTF-8");
    } catch (ClientProtocolException e) {
        System.err.println("Http协议出现问题");
        e.printStackTrace();
    } catch (ParseException e) {
        System.err.println("解析错误");
        e.printStackTrace();
    } catch (URISyntaxException e) {
        System.err.println("URI解析异常");
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IO异常");
        e.printStackTrace();
    } finally {
        // 释放连接
        if (null != response) {
            try {
                response.close();
                httpClient.close();
            } catch (IOException e) {
                System.err.println("释放连接出错");
                e.printStackTrace();
            }
        }
    }

    // 打印响应内容
    System.out.println(entityStr);

}

post:

@Test
public void postParams() {
    // 获取连接客户端工具
    CloseableHttpClient httpClient = HttpClients.createDefault();

    String entityStr = null;
    CloseableHttpResponse response = null;

    try {

        // 创建POST请求对象
        HttpPost httpPost = new HttpPost("http://www.baidu.com");

        /*
         * 添加请求参数
         */
        // 创建请求参数
        List<NameValuePair> list = new LinkedList<>();
        BasicNameValuePair param1 = new BasicNameValuePair("name", "root");
        BasicNameValuePair param2 = new BasicNameValuePair("password", "123456");
        list.add(param1);
        list.add(param2);
        // 使用URL实体转换工具
        UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
        httpPost.setEntity(entityParam);

        /* 
         * 添加请求头信息
         */
        // 浏览器表示
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
        // 传输的类型
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");

       // 执行请求
    response = httpClient.execute(httpPost);
    // 获得响应的实体对象
    HttpEntity entity = response.getEntity();
    // 使用Apache提供的工具类进行转换成字符串
    entityStr = EntityUtils.toString(entity, "UTF-8");

    /** 此处获取所有的响应头信息并进行打印 */
    System.out.println(Arrays.toString(response.getAllHeaders()));

} catch (ClientProtocolException e) {
    System.err.println("Http协议出现问题");
    e.printStackTrace();
} catch (ParseException e) {

        System.err.println("解析错误");
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IO异常");
        e.printStackTrace();
    } finally {
        // 释放连接
        if (null != response) {
            try {
                response.close();
                httpClient.close();
            } catch (IOException e) {
                System.err.println("释放连接出错");
                e.printStackTrace();
            }
        }
    }

    // 打印响应内容
    System.out.println(entityStr);
}

 

 

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
设置属性(设置请求头)

request.Method = "POST"; //GET或POST请求
request.ContentType = "application/x-www-form-urlencoded"; //Content-Type头
request.ContentLength = postDataStr.Length; //设置Content-Length头

 

常用属性


Method    请求方式

ContentType    Content-Type头

ContentLength    Content-Length头,正文的长度

Accept    Accept头,接受的类型

KeepAlive     是否保持HTTP连接

Headers    请求头的集合,可以添加/修改/删除某个请求头

UserAgent    User-Agent头,用于设置浏览器名

Proxy    获取或设置代理服务器的信息
常用函数方法


获取请求流(一般用于添加表单数据等)

request.GetRequestStream();


获取响应对象

HttpWebResponse response = (HttpWebResponse)request.GetResponse();


添加请求头

request.Headers.Add("xxx", val); //添加请求头xxx,的值为val

 

 

 

 

 

 

posted @ 2020-01-07 16:42  芮源  阅读(194)  评论(0)    收藏  举报