java 通过HttpClient调用第三方接口

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("失败!");
        }
    }
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值