JAVA HttpClient study notes (2): POST form parameter sending, configuration request information

Use HttpPost to create httpPost objects;

HttpPost httpPost=new HttpPost();

To prevent network disconnection, etc., configure the request information and set the maximum time limit

 //配置请求信息
        RequestConfig config=RequestConfig.custom().setConnectTimeout(1000) //创建链接的最长时间 ms
        .setConnectionRequestTimeout(1000) //设置获取链接的最长时间 ms
        .setSocketTimeout(10*1000)  //设置数据传输的最长时间
        .build(); //得到Requsetconfig

Configure the post URI, and configure the configuration information to Post

//给请求设置配置
        httpPost.setConfig(config);
        URIBuilder uriBuilder=new URIBuilder("www.hll520.cn");
        httpPost.setURI(uriBuilder.build());

Set the form parameters of Post, multiple parameters can add multiple BasicNameValuePair

 //创建List集合,封装表单请求参数
        List<NameValuePair> pairs=new ArrayList<>();
        pairs.add(new BasicNameValuePair("keys","C#"));//BasicNameValuePair放入键值对
        //创建表单的Entity对象
        UrlEncodedFormEntity formEntity=null;
 try {
            //参数1是封装好的表单数据,参数二是编码格式
            formEntity=new UrlEncodedFormEntity(pairs,"UTF-8"); //使UTF-8封装
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

Package parameters into post

//表单的Entity对象放入到post中
        httpPost.setEntity(formEntity);
  try {
            response=httpClient.execute(httpPost);//执行
        } catch (IOException e) {
            e.printStackTrace();
        }

Complete code

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class HttpPostDome {
    //直接模拟
    public static void main(String[] a) {
        //生成一个可关闭的HTTP浏览器(相当于)
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        //创建http POST请求
        HttpPost httpPost = new HttpPost("http://www.5iacg.cn/");
        //配置请求信息
        RequestConfig config = RequestConfig.custom().setConnectTimeout(1000) //创建链接的最长时间 ms
                .setConnectionRequestTimeout(1000) //设置获取链接的最长时间 ms
                .setSocketTimeout(10 * 1000)  //设置数据传输的最长时间
                .build(); //得到Requsetconfig
        //给请求设置配置
        httpPost.setConfig(config);
        //配置请求头,模拟火狐浏览器
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0");
        //创建List集合,封装表单请求参数
        List<NameValuePair> pairs = new ArrayList<>();
        pairs.add(new BasicNameValuePair("keys", "C#"));//BasicNameValuePair放入键值对
        //创建表单的Entity对象
        UrlEncodedFormEntity formEntity = null;
        try {
            //参数1是封装好的表单数据,参数二是编码格式
            formEntity = new UrlEncodedFormEntity(pairs, "UTF-8"); //使UTF-8封装
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //表单的Entity对象放入到post中
        httpPost.setEntity(formEntity);

        try {
            response = httpClient.execute(httpPost);//执行
        } catch (IOException e) {
            e.printStackTrace();
        }

        //获取网页源码
        HttpEntity httpEntity = response.getEntity();//获取网页源码
        try {
            String h = EntityUtils.toString(httpEntity, "UTF-8");//指定编码避免乱码
            System.out.println(h);
        } catch (IOException e) {
            //io异常(网络问题)
            e.printStackTrace();
        }

        //关闭HTTp
        try {
            response.close();
            httpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

result

Published 6 original articles · won 7 · views 255

Guess you like

Origin blog.csdn.net/XiaoYunKuaiFei/article/details/105414599