1

今天项目上碰到一个请求接口获取到报文中某个参数,将这个参数放到另一个请求上拼接,完成请求,获取其报文数据入库的需求,刚开始一脸懵啊!这个该怎么做啊(技术小白)...就网上各种百度呀,脑壳疼呀。终于....
开始:首先定位maven坐标,pom文件中添加:

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>

这时候就可以使用httpclinent了。

          get请求:
  public static  void httpClientGetTest() throws URISyntaxException {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //无参url
        //HttpGet httpGet = new HttpGet("http://www.baidu.com");
        //有参url后面的addParameter,可以无限的追加不用使用&分割自动添加;

    URIBuilder uriBuilder = new URIBuilder("http://www.baidu.com").addParameter("wd", "新闻");
    HttpGet httpGet = new HttpGet(uriBuilder.toString());
    try {
            //发起请求,获取结果
            CloseableHttpResponse response= httpClient.execute(httpGet);
            //返回值200为响应成功
            if (response.getStatusLine().getStatusCode() == 200) {
                //获取响应实体
                HttpEntity entity = response.getEntity();
                //转码
                String utf8 = EntityUtils.toString(entity, "utf8");
                System.out.println(utf8);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
}

    post 用法
  public static void httpClientPostTest() throws UnsupportedEncodingException {

    //创建httpClient对象
    CloseableHttpClient aDefault = HttpClients.createDefault();

    //设置发送表单内容数据
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
    //使用NameValuePair的实现类添加键值对的参数 键值对格式
    nameValuePairs.add(new BasicNameValuePair("wd","音乐"));

    //创建表单entity对象就是封装好的参数的bean,前面是参数后面是编码
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairs, "utf8");
    //创建Post请求对象
    HttpPost httpPost = new HttpPost("http://www.baidu.com");

}

代码也附加一下吧,欢迎各位指出问题

     

  @Service  
  public class TaskService {  

  @Autowired  
  private Amapper a;  
  @Autowired  
  private BMapper b;  
public String httpClint(String url) throws Exception {  
    CloseableHttpClient httpClient = HttpClients.createDefault();  
    URIBuilder uriBuilder = null;  
    uriBuilder = new URIBuilder(url);  
    HttpGet httpGet = new HttpGet(uriBuilder.toString());  
    //发起请求,获取结果  
    int isSuccess = 1;  
    while (isSuccess == 1) {  
        try {  
            CloseableHttpResponse response = null;  
            response = httpClient.execute(httpGet);  
            //返回值200为响应成功  
      int statusCode = response.getStatusLine().getStatusCode();  
            if (statusCode == 200) {  
                isSuccess = 0;  
                //获取响应实体  
       HttpEntity entity = response.getEntity();  
                //转码  
       String utf8 = EntityUtils.toString(entity, "utf8");  
                return utf8;  
            } else {  
                System.out.println("失败重试中...原因:响应码:" \+ statusCode);  
                Thread.sleep(3000);  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
            System.out.println("失败重试中...原因:" \+ e.getMessage());  
            Thread.sleep(3000);  
        }  

    }  
    return null;  
}  

public void httpClientGetTest() throws Exception {  
    GdZtHz gdZtHz = new GdZtHz();  

    Date now = new Date();  
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");//可以方便地修改日期格式  
String hah = dateFormat.format(now);  

    String result\_juid = httpClint("http://12.12.12.120/dsst.jhtml?account=11&password=22");  
    JSONObject jsonObject = JSON.parseObject(result\_juid);  
    String data = jsonObject.getString("data");  
    JSONObject jsondata = JSON.parseObject(data);  
    String token = jsondata.getString("juid");  
 String result\_pages = httpClint("http://1.2.3.4/dsse.jhtml?serv=g&spqsp=1805187359&serv=1&ju=" + token);  
    JSONObject jsonObject1 = JSON.parseObject(result\_pages);  
    String data1 = jsonObject1.getString("page");  
    JSONObject jsondata1 = JSON.parseObject(data1);  
    Integer token1 = Integer.parseInt(jsondata1.getString("pages\_"));  
    Integer total\_ = Integer.parseInt(jsondata1.getString("total\_"));  
    for (int i = 1; i <= token1; i++) {  
        long start = System.currentTimeMillis();  
        try {  
            Thread.sleep(3000);  
            System.out.println("睡眠3秒过去了");  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
        long end = System.currentTimeMillis();  
        String result = httpClint("http://1.2.3.4/dsse.jhtml?servi=getAdanfo&spqqid=1805187359&seriv=1&jud=" + token + "&page=" + i);  
        JSONObject jsonObject2 = JSON.parseObject(result);  
        JSONArray ja = jsonObject2.getJSONArray("data");  
 //遍历数组  
  for (int s = 0; s < ja.size(); s++) {  
            JSONObject jo = ja.getJSONObject(s);  
            String stockPercent = jo.getString("stockPercent");  
            String startDate = jo.getString("startDate");  
           );  
            String regNo = jo.getString("regNo");  
            GdxxHis gdxxHis = new GdxxHis();  
            gdxxHis.setStockpercent(stockPercent);  
      // 数据库新增字段  
        gdxxHis.setSpqspqid(spqSpqid);  
            a.insert(gdxxHis);  
            gdZtHz.setPch(pch);  
            gdZtHz.setSpqspqid(spqSpqid);  

        }  
    }  
    gdZtHz.setTotal(String.valueOf(total_));  
    b.insert(gdZtHz);  
}  

}
嗯!就是这样没错了,具体问题再具体分析吧,目前我还没有碰到其他的,告辞......


youjian
91 声望3 粉丝

emmmmmmmm