HttpClient--02--调用第三方接口

第三方接口

京东万象

HttpClient--02--调用第三方接口_第1张图片
HttpClient--02--调用第三方接口_第2张图片

  • 接口文档

HttpClient--02--调用第三方接口_第3张图片
HttpClient--02--调用第三方接口_第4张图片

原生实现:(java.net)

手机号码归属地查询

代码实现:

//Url:https://way.jd.com/jisuapi/query4?shouji=18711624642&appkey=184c54adfa929bc5e7160a2ba97f7e54
package com.cy.api;

import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;

public class HttpTest {

    public static void main(String[] args) throws IOException {

        //1.定义需要访问的地址
        URL url=  new URL("https://way.jd.com/jisuapi/query4");
        //2.连接url
        HttpURLConnection connection=(HttpURLConnection) url.openConnection();
        //3. 请求方式
        connection.setRequestMethod("POST");

        // 4.携带参数
        connection.setDoOutput(true);
        StringBuffer params=new StringBuffer();

        params.append("shouji=").append("1871162xxxxx")
                .append("&appkey=").append("184c54adfa929bc5e7160a2ba97f7e54");

        connection.getOutputStream().write(params.toString().getBytes("UTF-8"));

        //5.发起请求
        connection.connect();

        //6.接收返回值
        String response= StreamUtils.copyToString(connection.getInputStream(), Charset.forName("UTF-8"));
        System.out.println(response);


    }


}

在这里插入图片描述

编辑工具类HttpUtil

package com.cy.api;
import org.springframework.util.StreamUtils;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Map;

public class HttpUtil {
    public static String sendHttpRequest(String httpURL, Map<String,String> params) throws Exception {
        //1.定义需要访问的地址
        URL url=  new URL(httpURL);
        //2.连接url
        HttpURLConnection connection=(HttpURLConnection) url.openConnection();
        //3. 请求方式
        connection.setRequestMethod("POST");

        // 4.携带参数
        connection.setDoOutput(true);
        
       //校验参数
        if(params!=null && params.size()>0){
            
            StringBuffer sb=new StringBuffer();

            for (Map.Entry<String, String> param : params.entrySet()) {
               sb.append("&").append(param.getKey()).append("=").append(param.getValue());
            }

            //去除第一个
            connection.getOutputStream().write(sb.substring(1).getBytes("UTF-8"));
        }
        

        //5.发起请求
        connection.connect();

        //6.接收返回值
        String response= StreamUtils.copyToString(connection.getInputStream(), Charset.forName("UTF-8"));

           return response;
    }
}

天气预报查询

HttpClient--02--调用第三方接口_第5张图片
HttpClient--02--调用第三方接口_第6张图片

Url:https://way.jd.com/jisuapi/weather?city=安顺&cityid=111&citycode=101260301&appkey=184c54adfa929bc5e7160a2ba97f7e54

package com.cy.api;
import java.util.HashMap;

public class HttpTest {

    public static void main(String[] args) throws Exception {

        HashMap<String, String> params = new HashMap<>();
        params.put("city","北京");
        params.put("appkey","184c54adfa929bc5e7160a2ba97f7e54");

        String s = HttpUtil.sendHttpRequest("https://way.jd.com/jisuapi/weather", params);
        System.out.println(s);

    }
}

在这里插入图片描述

HttpClient–实现

1 引入jar包

 
        
            org.apache.httpcomponents
            httpclient
        

2.实现步骤:

  1. 确定目标地址:
  2. 创建httpClient客户端对象
  3. 创建请求类型
  4. 发起http请求.并且获取响应的结果
  5. 判断状态码是否为200
  6. 如果请求正确则动态获取响应值信息

3. 编辑测试API

package com.cy.api;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpTest02 {
    public static void main(String[] args) throws IOException {
        testGet();
    }

    public static void testGet() throws IOException {

       // 1.确定目标地址:
        String url = "https://way.jd.com/jisuapi/query4?shouji=18711624642&appkey=184c54adfa929bc5e7160a2ba97f7e54";

        //2.创建httpClient客户端对象
        HttpClient httpClient = HttpClients.createDefault();
        //3.创建请求类型
        HttpGet httpGet = new HttpGet(url);

        // 4.发起http请求.并且获取响应的结果
        HttpResponse httpResponse = httpClient.execute(httpGet);

        //5.判断状态码是否为200
        if(httpResponse.getStatusLine().getStatusCode() == 200) {

            //6.如果请求正确则动态获取响应值信息
            HttpEntity httpEntity = httpResponse.getEntity();
            String result = EntityUtils.toString(httpEntity, "UTF-8");
            System.out.println(result);
        }
    }
}

你可能感兴趣的:(项目-demo---技术点,java)