Reputation: 1849
I am developing a very simple http bot. I am using the javax.net.ssl.HttpsURLConnection
class and I have to make multiple requests.
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. 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)?
Thanks in advance
Upvotes: 0
Views: 10859
Reputation: 116908
I've never used HttpURLConnection
before. I usually use Apache's HTTPClient code. There are a lot of docs and tutorials about it on their home page.
Couple of things that I noticed about your code:
Cookie
headers on the response. Mine seems to handle that better.JSESSION
? Maybe there are other cookies you are missing?JSESSION
cookie gets set appropriately? I added some trim()
calls in my cookie processing code to make sure some spaces didn't slip in there.secondParameters
. I have no idea if they are valid. Have you debugged your code to verify the secondParamters
value looks good. You can see in my code what I'm posting to the server. Btw, I'd use a StringBuilder
instead of +
to build them.Hope this helps.
Upvotes: 1