欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 通过HttpClient调用第三方接口

程序员文章站 2022-06-27 11:14:33
...

java 通过HttpClient调用第三方接口

  1. post请求
    passStr:请求第三方接口所对应的参数值
    Properties.getRequestUrl() :参考我的另一篇博客:https://blog.csdn.net/u010596545/article/details/80355389
public static JSONObject doPost(String passStr) {
        //读取实验结果
        JSONObject retJson = new JSONObject();
        String mach = (passStr);
        String url = Properties.getRequestUrl() + mach;
        String sr = null;
        try {
            JSONObject sendJsn = new JSONObject();
            sr = sendPost(url, passStr);
            retJson = JSONObject.fromObject(sr);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return retJson;
    }

    public static String sendPost(String urlPath, String json) {
        String result = "";
        // 创建url资源
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(urlPath);
            post.setHeader("Content-Type", "application/x-www-form-urlencoded");

            List<NameValuePair> list = new ArrayList<NameValuePair>();
            list.add(new BasicNameValuePair("mach", json)); //该mach和请求的路径参数名称对应,例如:http://www.baidu.com/abbplat?mach=,该值是mach,若http://www.www.com/abbplat?php=,该值则是php
            post.setEntity(new UrlEncodedFormEntity(list, "utf-8"));

            HttpResponse httpResponse = client.execute(post);
            InputStream in = httpResponse.getEntity().getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
            StringBuilder strber = new StringBuilder();
            String line = null;
            while ((line = br.readLine()) != null) {
                strber.append(line + "\n");
            }
            in.close();
            result = strber.toString();
            if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                result = "服务器异常";
            }
        } catch (Exception e) {
            System.out.println("请求异常");
            throw new RuntimeException(e);
        }

        JSONObject rest = new JSONObject();
        rest = JSONObject.fromObject(result);
        return result;
    }
  1. get请求
 public static void httpURLConectionGET() {
        try {
            URL url = new URL("第三方请求路径");    //把字符串转换为URL请求地址
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接		
           	//放在请求头中
            connection.setRequestProperty("token", "token值");
            connection.connect();// 连接会话
            // 获取输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {// 循环读取流
                sb.append(line);
            }
            br.close();// 关闭流
            connection.disconnect();// 断开连接
            System.out.println(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("失败!");
        }
    }