http://zeusami.iteye.com/blog/1172864

  1. package com.alibaba.xteam.web.travel.module.rpc;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.PrintWriter;
  8. import java.net.URL;
  9. import java.net.URLConnection;
  10. import java.util.List;
  11. import java.util.Map;
  12.  
  13. import org.apache.http.client.ClientProtocolException;
  14.  
  15. public class PayTest {
  16.  
  17. public static void main(String[] args) throws ClientProtocolException, IOException {
  18.  
  19. for(int i=0;i<10;i++){
  20. new Thread(new Runnable(){
  21.  
  22. @Override
  23. public void run() {
  24.  
  25. //发送 GET 请求
  26. String s1=PayTest.sendGet("http://10.125.25.187/dingtalk/hx/index.do","debug=y");
  27.  
  28. String s2=PayTest.sendGet("http://10.125.25.187/open/didi/pay/pay_notify.json", "orderId=5675551694383500500&normalDistance=11&totalPrice=11.33&status=700&callbackInfo=3831,221");
  29.  
  30. //String s2=PayTest.sendGet("http://10.125.25.187/dingtalk/dingtalk/taxi/special/createOrder.jsonp","__Order.create()__=&passengerGeotype=wgs84&serviceType=KC&carLevel=KC&cityId=5&appTime=1464100743424&fromCityName=%E6%9D%AD%E5%B7%9E%E5%B8%82&fromAddr=%E9%98%BF%E9%87%8C%E5%B7%B4%E5%B7%B4%E8%A5%BF%E6%BA%AA%E5%9B%AD%E5%8C%BA&fromAddrDetail=&fromLongitude=120.025459&fromLatitude=30.2791019&toCityName=%E6%9D%AD%E5%B7%9E%E5%B8%82&toAddr=%E5%98%89%E7%A6%BE%E8%8A%B1%E8%8B%918%E5%B9%A2&toLongitude=120.096661&toLatitude=30.272399&passengerMobile=15088688387&businessCategory=WORK&memo=uu&timeType=IN_TIME&_=1464100743427&callback=jsonp");
  31. System.out.println(s1);
  32.  
  33. }
  34.  
  35. }
  36. ).start();
  37.  
  38. }
  39.  
  40. }
  41.  
  42. public static String convertStreamToString(InputStream is) {
  43. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  44. StringBuilder sb = new StringBuilder();
  45.  
  46. String line = null;
  47. try {
  48. while ((line = reader.readLine()) != null) {
  49. sb.append(line + "\n");
  50. }
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. } finally {
  54. try {
  55. is.close();
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. return sb.toString();
  61. }
  62.  
  63. /**
  64. * 向指定URL发送GET方法的请求
  65. *
  66. * @param url 发送请求的URL
  67. * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  68. * @return URL 所代表远程资源的响应结果
  69. */
  70. public static String sendGet(String url, String param) {
  71. String result = "";
  72. BufferedReader in = null;
  73. try {
  74. String urlNameString = url + "?" + param;
  75. URL realUrl = new URL(urlNameString);
  76. // 打开和URL之间的连接
  77. URLConnection connection = realUrl.openConnection();
  78. // 设置通用的请求属性
  79. connection.setRequestProperty("accept", "*/*");
  80. connection.setRequestProperty("connection", "Keep-Alive");
  81. connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  82. // 建立实际的连接
  83. connection.connect();
  84. // 获取所有响应头字段
  85. Map<String, List<String>> map = connection.getHeaderFields();
  86. // 遍历所有的响应头字段
  87. for (String key : map.keySet()) {
  88. System.out.println(key + "--->" + map.get(key));
  89. }
  90. // 定义 BufferedReader输入流来读取URL的响应
  91. in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  92. String line;
  93. while ((line = in.readLine()) != null) {
  94. result += line;
  95. }
  96. } catch (Exception e) {
  97. System.out.println("发送GET请求出现异常!" + e);
  98. e.printStackTrace();
  99. }
  100. // 使用finally块来关闭输入流
  101. finally {
  102. try {
  103. if (in != null) {
  104. in.close();
  105. }
  106. } catch (Exception e2) {
  107. e2.printStackTrace();
  108. }
  109. }
  110. return result;
  111. }
  112.  
  113. /**
  114. * 向指定 URL 发送POST方法的请求
  115. *
  116. * @param url 发送请求的 URL
  117. * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  118. * @return 所代表远程资源的响应结果
  119. */
  120. public static String sendPost(String url, String param) {
  121. PrintWriter out = null;
  122. BufferedReader in = null;
  123. String result = "";
  124. try {
  125. URL realUrl = new URL(url);
  126. // 打开和URL之间的连接
  127. URLConnection conn = realUrl.openConnection();
  128. // 设置通用的请求属性
  129. conn.setRequestProperty("accept", "*/*");
  130. conn.setRequestProperty("connection", "Keep-Alive");
  131. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  132. // 发送POST请求必须设置如下两行
  133. conn.setDoOutput(true);
  134. conn.setDoInput(true);
  135. // 获取URLConnection对象对应的输出流
  136. out = new PrintWriter(conn.getOutputStream());
  137. // 发送请求参数
  138. out.print(param);
  139. // flush输出流的缓冲
  140. out.flush();
  141. // 定义BufferedReader输入流来读取URL的响应
  142. in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  143. String line;
  144. while ((line = in.readLine()) != null) {
  145. result += line;
  146. }
  147. } catch (Exception e) {
  148. System.out.println("发送 POST 请求出现异常!" + e);
  149. e.printStackTrace();
  150. }
  151. // 使用finally块来关闭输出流、输入流
  152. finally {
  153. try {
  154. if (out != null) {
  155. out.close();
  156. }
  157. if (in != null) {
  158. in.close();
  159. }
  160. } catch (IOException ex) {
  161. ex.printStackTrace();
  162. }
  163. }
  164. return result;
  165. }
  166.  
  167. }

并发调用get请求的更多相关文章

  1. 异步编程CompletableFuture实现高并发系统优化之请求合并

    先说场景: 根据Redis官网介绍,单机版Redis的读写性能是12万/秒,批量处理可以达到70万/秒.不管是缓存或者是数据库,都有批量处理的功能.当我们的系统达到瓶颈的时候,我们考虑充分的压榨缓存和 ...

  2. Swoole 协程的并发调用及使用示例

    示例一: 利用通道pop会自动挂起当前协程,等待生产者推送数据的特性,实现并发调用,并在协程完成后组合结果集. $serv = new Swoole\Http\Server("127.0.0 ...

  3. java调用http请求json

    最近遇到的问题,java如何调用http请求json: public class HttpClientUtil { private static final String CONTENT_TYPE_T ...

  4. SpringMVC,MyBatis项目中兼容Oracle和MySql的解决方案及其项目环境搭建配置、web项目中的单元测试写法、HttpClient调用post请求等案例

     要搭建的项目的项目结构如下(使用的框架为:Spring.SpingMVC.MyBatis): 2.pom.xml中的配置如下(注意,本工程分为几个小的子工程,另外两个工程最终是jar包): 其中 ...

  5. wpf 错误 执行了 QueryInterface 调用,请求提供 COM 可见的托管类“BoilerMonitoringV1._0.MapControl”的默认 IDispatch 接口。

    在做wpf嵌入地图时,在自定义的WebBrowser 里面使用JavaScript调用外部方法的时报的错误 在原来的WinForm里 我们只要在窗体类设置的头部设置个 [System.Runtime. ...

  6. uni-app如何解决在for循环里调用异步请求获取数据顺序混乱问题?

    总结/朱季谦 先前有一次做uni-app的js接口对接时,遇到过这样的情况,在for循环里,调用一个异步请求时,返回来的值顺序是乱的,因此,在以下的代码里,push到数组里的值,每次的顺序可能都是不一 ...

  7. ASW 工作流最佳实践(二):使用 ASW 并发调用函数

    在音视频转码.ETL 作业处理.基因数据处理等诸多场景中,我们都可以通过工作流并行调用云函数,将任务进行并行处理,大大提高任务处理的吞吐量,满足应用场景的高实时性.高并发能力. 在<使用 ASW ...

  8. 并发的HTTP请求,apache是如何响应的,以及如何调用php文件的

    作者:酒窝链接:https://www.zhihu.com/question/23786410/answer/153455460来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  9. 在使用HttpClient做客户端调用一个API时 模拟并发调用时发生“死锁"?

    平时还是比较喜欢看书的..但有时候遇到问题还是经常感到脑袋一蒙..智商果然是硬伤.. 同事发现了个问题,代码如下: class Program { static void Main(string[] ...

随机推荐

  1. 翻译 Tri-Ace:在Shader里近似渲染公式

    继上一篇:次世代基于物理渲染的反射模型,本篇是Tri-Ace 在cedec2014上最近发布的, 主要内容如名称所示,解释了他们在实现基于物理渲染时,对shader的渲染公式所做的近似工作.     ...

  2. xml提取

    $url = 'http://221.232.141.108/hsdcw/news.xml'; $opts = array( 'http'=>array( 'method'=>" ...

  3. instruction-set architecture Processor Architecture

    Computer Systems A Programmer's Perspective Second Edition We have seen that a processor must execut ...

  4. Java Phaser

    //Listing 6-5. Using a Phaser to Control a One-Shot Action Serving a Variable Number //of Parties im ...

  5. android source compiler

  6. iOS archive(归档)的总结 (序列化和反序列化,持久化到文件)

    http://www.cnblogs.com/ios8/p/ios-archive.html

  7. VS2015 多项目源码共享链接

    Eclipse有这个功能,在一个项目中加入另一个项目文件夹的引用,源码包含过来,这样不必copy一份代码,只需要维护一份源代码.一直想在VS中找到这个功能,目前项目需要,终于google到了. htt ...

  8. PHP运行错最有效解决办法Fatal error: Out of memory (allocated 786432) (tried to allocate 98304 bytes) in H:\freehost\zhengbao2\web\includes\lib_common.php on line 744

    原文 PHP运行错最有效解决办法Fatal error: Out of memory (allocated 6029312) Fatal error: Out of memory (allocated ...

  9. [LeetCode]题解(python):052-N-Queens II

    题目来源 https://leetcode.com/problems/n-queens-ii/ Follow up for N-Queens problem. Now, instead outputt ...

  10. 常用 linux 命令(部分)

    常用命令总结: 1. mkdir命令 mkdir dirname , 用来创建目录.该命令创建由dirname命名的目录.如果在目录名的前面没有加任何路径名,则在当前目录下创建由dirname指定的目 ...