[英]Multiple Http requests and responses using HttpURLConnection
Good afternoon in my timezone. 我所在的时区下午好。
I am developing an very simple http bot. 我正在开发一个非常简单的http bot。 I am using the javax.net.ssl.HttpsURLConnection class and i have to make multiple requests.
我正在使用javax.net.ssl.HttpsURLConnection类,并且我必须发出多个请求。 Snippet of code :
代码片段:
HttpURLConnection urlConnection =
(HttpURLConnection) new URL(url+"?"+firstParameters).openConnection();
urlConnection.setRequestProperty("Accept-Charset", "UTF-8");
headerFields = urlConnection.getHeaderFields();
keys = headerFields.keySet();
for(String key : keys){
if(key != null && key.contains("ookie")){
cookies = urlConnection.getHeaderField(key);
break;
}
}
for(String cookie : cookies.split(";")){
if(cookie.contains("JSESSION")){
JSESSION = cookie.split("=")[1];
break;
}
}
document = new InputSource(urlConnection.getInputStream());
parser.setDocument(document);
attributesId.put("name",new ArrayList<String>(Arrays.asList(attributesNames)));
elementsIds.put("INPUT",attributesId);
elements = parser.getValues(elementsIds);
for(String attr : attributesNames){
secondParameters = secondParameters.replaceAll("#r"+index,elements.get(attr));
}
urlConnection.getInputStream().close();
//Second call
urlConnection = (HttpURLConnection) new URL(url2).openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Cookie", "JSESSIONID="+JSESSION);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
payload = new PrintWriter(urlConnection.getOutputStream());
payload.print(secondParameters);
payload.flush();
payload.close();
Summarizing the code above, first i do a request without any payload and i am able to see the correct response from the server, but the problem is when i make the second request (now with payload and with the JSESSION cookie), what i receive it his the same response that i received in the first request, it looks like i am making the first request again. 总结上面的代码,首先我执行一个没有任何有效负载的请求,并且能够看到来自服务器的正确响应,但是问题是当我发出第二个请求(现在带有有效负载和JSESSION cookie)时,我收到了什么它与我在第一个请求中收到的响应相同,好像我在再次发出第一个请求。 So my question is , what i am doing wrong ?
所以我的问题是,我在做什么错? I just need to open one connection, and then change the headers and payload ?
我只需要打开一个连接,然后更改标题和有效负载? There is any tutorial related with multiple http requests(with mixed methods , post and get)?
有没有与多个http请求相关的教程(使用混合方法,post和get)?
Thanks in advance 提前致谢
With the best regards 致以最诚挚的问候
I've never used HttpURLConnection
before. 我以前从未使用过
HttpURLConnection
。 I usually use Apache's HTTPClient code . 我通常使用Apache的HTTPClient代码 。 There are a lot of docs and tutorials about it on their home page.
在他们的主页上有很多关于它的文档和教程。
I couldn't find any tutorials about HttpURLConnection
but I tried it out. 我找不到有关
HttpURLConnection
任何教程,但尝试了一下。 Here's my little program which is similar to yours. 这是我的小程序,与您的程序相似。
It seems to work: 似乎有效:
Couple of things that I noticed about your code: 关于您的代码,我注意到了几件事:
Cookie
headers on the response. Cookie
标头。 Mine seems to handle that better. JSESSION
? JSESSION
吗? Maybe there are other cookies you are missing? JSESSION
cookie gets set appropriately? JSESSION
cookie? I added some trim()
calls in my cookie processing code to make sure some spaces didn't slip in there. trim()
调用,以确保其中没有空格。 secondParameters
. secondParameters
的真正价值。 I have no idea if they are valid. secondParamters
value looks good. secondParamters
值看起来不错。 You can see in my code what I'm posting to the server. StringBuilder
instead of +
to build them. StringBuilder
而不是+
来构建它们。 Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.