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

package com.alibaba.xteam.web.travel.module.rpc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map; import org.apache.http.client.ClientProtocolException; public class PayTest { public static void main(String[] args) throws ClientProtocolException, IOException { for(int i=0;i<10;i++){
new Thread(new Runnable(){ @Override
public void run() { //发送 GET 请求
String s1=PayTest.sendGet("http://10.125.25.187/dingtalk/hx/index.do","debug=y"); 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"); //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");
System.out.println(s1); } }
).start(); } } public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder(); String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
} /**
* 向指定URL发送GET方法的请求
*
* @param url 发送请求的URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
*/
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
} /**
* 向指定 URL 发送POST方法的请求
*
* @param url 发送请求的 URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
} }

并发调用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. PHP文件操作 之统计目录大小

    <?php //定义一个函数 统计目录大小函数 function dirSize($dirName) { //判断目录是否存在 if (!file_exists($dirName)) { die ...

  2. Euler's totient function

    https://en.wikipedia.org/wiki/Euler's_totient_function counts the positive integers up to a given in ...

  3. Locking

    Computer Science An Overview _J. Glenn Brookshear _11th Edition To solve such problems, a DBMS could ...

  4. Open Sourcing Kafka Monitor

    https://engineering.linkedin.com/blog/2016/05/open-sourcing-kafka-monitor     https://github.com/lin ...

  5. [收藏]Asp.net MVC生命周期

    一个HTTP请求从IIS移交到Asp.net运行时,Asp.net MVC是在什么时机获得了控制权并对请求进行处理呢?处理过程又是怎样的? 以IIS7中asp.net应用程序生命周期为例,下图是来自M ...

  6. NRF51822之pstorage介绍

    This information applies to the following SoftDevices: S110, S120, S130, S310 Introduction Persisten ...

  7. hadoop、hbase、hive、zookeeper版本对应关系

    本文引用自:http://www.aboutyun.com/blog-61-62.html 最新版本: hadoop和hbase版本对应关系: Hbase    Hadoop 0.92.0 1.0.0 ...

  8. Qt 2D绘图 渐变填充(三种渐变方式)

    在qt中提供了三种渐变方式,分别是线性渐变,圆形渐变和圆锥渐变.如果能熟练应用它们,就能设计出炫目的填充效果. 线性渐变: 1.更改函数如下: void Dialog::paintEvent(QPai ...

  9. IOS测试程序运行耗时

    iOS设备相对于电脑,内存和处理能力有限,所以一段代码或者程序运行的时间需要时刻注意,这里提供两种获取精确时间的方法. 方法一:使用系统时间 NSDate* tmpStartData = [[NSDa ...

  10. 20145211 《Java程序设计》第6周学习总结——三笑徒然当一痴

    教材学习内容总结 I/O--InputStream与OutStream Java中I/O操作主要是指使用Java进行输入,输出操作.这与c++中的iostream并无太大区别. Java所有的I/O机 ...