Redian新闻
>
java http request能做到打开一次多次发送http request吗
avatar
java http request能做到打开一次多次发送http request吗# Java - 爪哇娇娃
d*8
1
rt
avatar
D*0
2
有很多连续的http request,如果每次都重新open一个connection,performance下降
很明显,能不能打开一次,然后多次发送http request呢?放狗了一下,没发现什么方
法,keep-alive好像是默认的。有人说要从tcp socket开始自己写http协议。不知道有
没有做过这方面的大侠指点一下,一下是我测试的代码。这段代码如果每次都重新call
HttpsURLConnection con = HttpsURLConnection)myurl.openConnection(); 就没问
题,但是如果先这样,那个URL里只有第一个值(忘了说这个是REST)。谢谢。
String httpsURL = "https://xxxx.yyyy.com/users.json";
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
con.setRequestMethod("PUT");
con.setDoOutput(true);
con.setDoInput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream()
);
JSONObject json = new JSONObject();
json.put("first", "xxx");
json.put("last", "yyy");
System.out.println(json.toString());
output.writeBytes(json.toString());
output.flush();

json.put("first", "aaa");
json.put("last", "bbbb");
System.out.println(json.toString());
output.writeBytes(json.toString());
output.flush();

json.put("first", "eee");
json.put("last", "rrrr");
System.out.println(json.toString());
output.writeBytes(json.toString());
output.flush();
input = new DataInputStream( con.getInputStream() );
for( int c = input.read(); c != -1; c = input.read() )
System.out.print( (char)c );
System.out.println("Resp Code:"+con .getResponseCode());
System.out.println("Resp Message:"+ con .getResponseMessage());
input.close();
output.close();
con.disconnect();
avatar
w*o
3
re

【在 d********8 的大作中提到】
: rt
avatar
g*g
4
Try a client library such as Apache HttpClient instead of trying to reinvent
the wheel. It's easy to configure connection management from there.

call
);

【在 D***0 的大作中提到】
: 有很多连续的http request,如果每次都重新open一个connection,performance下降
: 很明显,能不能打开一次,然后多次发送http request呢?放狗了一下,没发现什么方
: 法,keep-alive好像是默认的。有人说要从tcp socket开始自己写http协议。不知道有
: 没有做过这方面的大侠指点一下,一下是我测试的代码。这段代码如果每次都重新call
: HttpsURLConnection con = HttpsURLConnection)myurl.openConnection(); 就没问
: 题,但是如果先这样,那个URL里只有第一个值(忘了说这个是REST)。谢谢。
: String httpsURL = "https://xxxx.yyyy.com/users.json";
: URL myurl = new URL(httpsURL);
: HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
: con.setRequestMethod("PUT");

avatar
d*8
5
社嘛意思?

【在 w******o 的大作中提到】
: re
avatar
g*w
7
版上有多少钱?

【在 d********8 的大作中提到】
: rt
avatar
D*0
8
thanks,好虫兄,那这种open连接一次,多次发送request理论上可以行否?

reinvent

【在 g*****g 的大作中提到】
: Try a client library such as Apache HttpClient instead of trying to reinvent
: the wheel. It's easy to configure connection management from there.
:
: call
: );

avatar
d*8
9
3块多钱

【在 g**w 的大作中提到】
: 版上有多少钱?
avatar
w*o
11
re包子

【在 d********8 的大作中提到】
: 社嘛意思?
avatar
g*g
12
sure

【在 D***0 的大作中提到】
: thanks,好虫兄,那这种open连接一次,多次发送request理论上可以行否?
:
: reinvent

avatar
g*w
13
。。。都发给谁了啊

【在 d********8 的大作中提到】
: 3块多钱
avatar
g*e
14
use PoolingClientConnectionManager

【在 D***0 的大作中提到】
: thanks,好虫兄,那这种open连接一次,多次发送request理论上可以行否?
:
: reinvent

avatar
d*8
15
我来之前,总共80块钱。
都发完了,
其余的,都是我自掏腰包。
都不知道给谁了。

【在 g**w 的大作中提到】
: 。。。都发给谁了啊
avatar
D*0
16
Apache commons HttpClient?

【在 g**e 的大作中提到】
: use PoolingClientConnectionManager
avatar
a*z
17
wow
钻风mm拨款了~
赞~

【在 d********8 的大作中提到】
: 我来之前,总共80块钱。
: 都发完了,
: 其余的,都是我自掏腰包。
: 都不知道给谁了。

avatar
D*0
18
谢谢,刚才写了段小程序测试,能写,但是performance一样没达到要求,不知道是我
写的问题还是,因为那个httpput还是每次都new,然后httpClient.execute( httpPut
);在发送完所有的data之后我才
if(mgr != null)
{
mgr.shutdown();
}
if(httpClient != null)
{
httpClient.getConnectionManager().shutdown();
}
不知道是不是因为每次都new httpput的原因。请指教。

【在 g**e 的大作中提到】
: use PoolingClientConnectionManager
avatar
g*w
19
。。。好像没给俺。。。
这几天挺水的,版面金库没有钱?

【在 d********8 的大作中提到】
: 我来之前,总共80块钱。
: 都发完了,
: 其余的,都是我自掏腰包。
: 都不知道给谁了。

avatar
o*2
20
Chunked Encoding is part of HTTP 1.1.

【在 D***0 的大作中提到】
: I can only control my side(client side).
avatar
p*o
21
是tm我给的

【在 a******z 的大作中提到】
: wow
: 钻风mm拨款了~
: 赞~

avatar
g*g
22
If the requests can be executed concurrently, you should do that along with
PoolingClientConnectionManager. If the requests have to be executed
sequentially, there's not much to optimize there beyond keep-alive which the
framework has taken care of. You may still be able to execute multiple
transactions concurrently and achieve high throughput.
Ning Async httpclient is a better option if you need to push the performance
to its extreme.

httpPut

【在 D***0 的大作中提到】
: 谢谢,刚才写了段小程序测试,能写,但是performance一样没达到要求,不知道是我
: 写的问题还是,因为那个httpput还是每次都new,然后httpClient.execute( httpPut
: );在发送完所有的data之后我才
: if(mgr != null)
: {
: mgr.shutdown();
: }
: if(httpClient != null)
: {
: httpClient.getConnectionManager().shutdown();

avatar
d*8
23
你给了多少? 给哪了?

【在 p***o 的大作中提到】
: 是tm我给的
avatar
D*0
24
谢谢,的确这些request不能concurrent,必须sequential,再去试试Ning Async
httpclient。谢谢好虫

with
the
performance

【在 g*****g 的大作中提到】
: If the requests can be executed concurrently, you should do that along with
: PoolingClientConnectionManager. If the requests have to be executed
: sequentially, there's not much to optimize there beyond keep-alive which the
: framework has taken care of. You may still be able to execute multiple
: transactions concurrently and achieve high throughput.
: Ning Async httpclient is a better option if you need to push the performance
: to its extreme.
:
: httpPut

avatar
a*z
25
真的呀,那在多给点吧~谢啦~

【在 p***o 的大作中提到】
: 是tm我给的
avatar
g*g
26
I don't think Async will help your case either. For a single transaction
with multiple sequential requests, the optimization is mostly on server side.

【在 D***0 的大作中提到】
: 谢谢,的确这些request不能concurrent,必须sequential,再去试试Ning Async
: httpclient。谢谢好虫
:
: with
: the
: performance

avatar
p*o
27
去版面上查一下就知道了,钻妹子那点钱也是我让她吐出来的,大家都知道我有个外号
叫老刑了

【在 d********8 的大作中提到】
: 你给了多少? 给哪了?
avatar
D*0
28
yeah, but the server is out of my control and I can do nothing to change it.

side.

【在 g*****g 的大作中提到】
: I don't think Async will help your case either. For a single transaction
: with multiple sequential requests, the optimization is mostly on server side.

avatar
b*i
29
你需要的指标到底怎样?比如,需要每秒钟几次?

call
);

【在 D***0 的大作中提到】
: 有很多连续的http request,如果每次都重新open一个connection,performance下降
: 很明显,能不能打开一次,然后多次发送http request呢?放狗了一下,没发现什么方
: 法,keep-alive好像是默认的。有人说要从tcp socket开始自己写http协议。不知道有
: 没有做过这方面的大侠指点一下,一下是我测试的代码。这段代码如果每次都重新call
: HttpsURLConnection con = HttpsURLConnection)myurl.openConnection(); 就没问
: 题,但是如果先这样,那个URL里只有第一个值(忘了说这个是REST)。谢谢。
: String httpsURL = "https://xxxx.yyyy.com/users.json";
: URL myurl = new URL(httpsURL);
: HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
: con.setRequestMethod("PUT");

avatar
s*e
30
there are four tcp/ip keep alive mechanisms (server push). They are Polling,
long polling, http streaming and web socket. Among them only websocket is a
real duplex mechanism. But unfortunately not all browsers support websocket
.
Frameworks can make the connection management transparent. try atmosphere
framework to see if it meets your need.
avatar
D*0
31
没有什么具体指标,越快越好,因为我希望做成real time update。数据都是根据
simulation结果产生的,所以没有每秒几次之说。

【在 b***i 的大作中提到】
: 你需要的指标到底怎样?比如,需要每秒钟几次?
:
: call
: );

avatar
D*0
32
谢谢指点,
恩,这四种其实最想使用web socket,以前弄过一段,但是就是因为浏览器的原因,就
没往这个方向走,其实主要还是想实现一个前台控制,后台push的结构。主要是计算都
在后台,前台实时更新显示和接收user各种控制点击。试过pushlet,也知道comet,但
是没在实际当中用过。感觉web socket是最符合要求的。现在这个问题是看见了有个
firebase的东东,看看能不能用它当中转,目前写的一些测试程序显示performance不
是很好,所以才来问问。

Polling,
a
websocket

【在 s******e 的大作中提到】
: there are four tcp/ip keep alive mechanisms (server push). They are Polling,
: long polling, http streaming and web socket. Among them only websocket is a
: real duplex mechanism. But unfortunately not all browsers support websocket
: .
: Frameworks can make the connection management transparent. try atmosphere
: framework to see if it meets your need.

avatar
o*2
33
楼主,你的问题解决了没有?
avatar
D*0
34
还在试验,现在还不知道是不是因为第三方的performance的原因,还是我的写法有问
题,还是java本身的performance的问题,谢谢关心

【在 o**2 的大作中提到】
: 楼主,你的问题解决了没有?
avatar
o*2
35
我觉得你这里有几个相关但独立的问题:
1,performance问题;
2,http protocol细节问题;
3,Java对http protocol的支持及其实现细节问题;
4,其他libraries对http protocol的支持及其实现细节问题。
我个人认为你最好把第二个问题弄清楚,这样就算performance问题最终解决不了,你
起码也学到了新的知识,对不对?
要弄清楚http的细节,特别是你的server特定的情况下的细节,你应该用socket来编测
试程序,这样你才能知道到底哪些bytes去了server,和哪些bytes从server来。这应该
不难,你用proxy软件就可以看到你的现有软件的http内容,然后用socket来发这些内
容。再根据http spec,修改或添加内容,并试验你的多request方案。

【在 D***0 的大作中提到】
: 还在试验,现在还不知道是不是因为第三方的performance的原因,还是我的写法有问
: 题,还是java本身的performance的问题,谢谢关心

avatar
D*0
36
其实发送和接收的都没问题,就是速度不能达到要求,用wireshark看看和server的
communication应该有帮助。谢谢指点,我会从这几方面查的。

【在 o**2 的大作中提到】
: 我觉得你这里有几个相关但独立的问题:
: 1,performance问题;
: 2,http protocol细节问题;
: 3,Java对http protocol的支持及其实现细节问题;
: 4,其他libraries对http protocol的支持及其实现细节问题。
: 我个人认为你最好把第二个问题弄清楚,这样就算performance问题最终解决不了,你
: 起码也学到了新的知识,对不对?
: 要弄清楚http的细节,特别是你的server特定的情况下的细节,你应该用socket来编测
: 试程序,这样你才能知道到底哪些bytes去了server,和哪些bytes从server来。这应该
: 不难,你用proxy软件就可以看到你的现有软件的http内容,然后用socket来发这些内

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。