Java_HttpRequest方法

  1. /**

  2. * http调用

  3. *

  4. * @param httpUrl :请求接口

  5. * @param httpArg :参数

  6. * @return 返回结果

  7. */

  8. public static String httpRequest(String httpUrl, String httpArg) {

  9. BufferedReader reader = null;

  10. String result = null;

  11. StringBuffer sbf = new StringBuffer();

  12. httpUrl = httpUrl + "?" + httpArg;

  13.  
  14. try {

  15. URL url = new URL(httpUrl);

  16. HttpURLConnection connection = (HttpURLConnection) url

  17. .openConnection();

  18. connection.setConnectTimeout(3000);//超时时间,单位毫秒,3000是3秒

  19. connection.setReadTimeout(3000);

  20. connection.setRequestMethod("GET");

  21. // 填入apikey到HTTP header(百度接口)

  22. //connection.setRequestProperty("apikey", "11111");

  23. connection.connect();

  24. InputStream is = connection.getInputStream();

  25. reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));//UTF-8

  26. String strRead = null;

  27. while ((strRead = reader.readLine()) != null) {

  28. sbf.append(strRead);

  29. sbf.append("\r\n");

  30. }

  31. reader.close();

  32. result = sbf.toString();

  33. } catch (Exception e) {

  34. e.printStackTrace();

  35. }

  36. return result;

  37. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值