简体   繁体   English

使用HttpURLConnection的多个Http请求和响应

[英]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. 这是我的小程序,与您的程序相似。

http://pastie.org/2706758 http://pastie.org/2706758

It seems to work: 似乎有效:

  1. It makes an initial request to get session cookie. 发出初始请求以获取会话cookie。
  2. It processes the response headers to get the cookie. 它处理响应头以获取cookie。
  3. It makes a second request to authenticate the session with the cookie in the request. 它发出第二个请求,以使用请求中的cookie对会话进行身份验证。
  4. If a correct username/password is used then the results show that the session is authenticated. 如果使用正确的用户名/密码,则结果表明会话已通过身份验证。

Couple of things that I noticed about your code: 关于您的代码,我注意到了几件事:

  • You code does not handle multiple Cookie headers on the response. 您的代码无法处理响应中的多个Cookie标头。 Mine seems to handle that better. 我的似乎更好。
  • Are you sure that all you need is JSESSION ? 您确定只需要JSESSION吗? Maybe there are other cookies you are missing? 也许您还缺少其他Cookie?
  • Have you debugged your code to make sure that your 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. 我在cookie处理代码中添加了一些trim()调用,以确保其中没有空格。
  • I can't see the real value of your secondParameters . 我看不到您secondParameters的真正价值。 I have no idea if they are valid. 我不知道它们是否有效。 Have you debugged your code to verify the secondParamters value looks good. 您是否已调试代码以验证secondParamters值看起来不错。 You can see in my code what I'm posting to the server. 您可以在我的代码中看到我要发布到服务器的内容。 Btw, I'd use a StringBuilder instead of + to build them. 顺便说一句,我将使用StringBuilder而不是+来构建它们。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM