On a practice project with JEE, I have an HTML page on Javascript code, that request to my local server, which search information in internet making requests GET and POST, depending the case. But it happens that after making a GET request, making up the POST request, it emerge an exception in execution of
connection.setRequestMethod("POST");
It comes out: "java.lang.IllegalStateException: Connect in progress". So then I had decided to close it in a try-catch block writing:
try{ con.setRequestMethod("POST"); }catch(Exception ex){}
But as I try to put the header request, which it has the object 'request' from the server it gives
"java.lang.IllegalStateException: Already connected"
But the only thing it is followed is a function that set the header as:
connection.setRequestProperty("header", "content");
If anybody can help me to resolve this I would be really thankful in advance.
I think the previous explanation of my problem was not clear enough. The function a use to make the requests, and to put the header request and the parameters with their values that have on the object 'request' is:
public static String obtener_html(String url, HttpServletRequest request,
ArrayList<Key_value> replacements, boolean get){
try {
HttpURLConnection connection;
if(get){ url = data(url, request); }
connection = (HttpURLConnection)(new URL(url).openConnection());
if(get){
connection.setRequestMethod("GET");
set_headers(request, connection, replacements);
if(connection.getDoOutput()){ connection.setDoOutput(false); }
}
else{
connection.setRequestMethod("POST");
set_headers(request, connection, replacements);
if(!connection.getDoOutput()){ connection.setDoOutput(true); }
DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
writer.writeBytes(data(null, request));
writer.flush();
writer.close();
}
int i = connection.getResponseCode();
if(i == 200){
InputStream reader = connection.getInputStream();
Text text = new Text();
int n;
while((n=reader.read()) != -1){ text.add((char)n); }
reader.close();
connection.disconnect();
return text.toString();
}
connection.disconnect();
}
catch(IOException ex){ ex.printStackTrace(); }
return null;
}
In my code:
- The class" Key_value", is made by me.
- The function "data(...)", get the parameters and the values from the object "request", and it put them into the "connection" object.
- The function "set headers", makes the same with the header request, but changing the headers request indicated .
- The class "Text", is made by me, to accelerate the consecutive/sequential insertion of characters from a text.
It turns out that the first request made by my server it’s a GET, but it gives and error message when it makes later a POST. Each time it is called, makes a "connection" object, and before the connection finish it is closed, but anyway it gives an error message. I only make requests to two IP address, but obviously the url’s can be different. In addition, even if it was not necessary to indicate that the request it will be "POST", because the flag that controls the method "setDoOutput(...)" is already "true". Why it doesn’t allow me to put the header request even if I enclose it in a try-catch block? How can I resolve this? The exception says that the connection is in progress, or that it’s already connected, like if the object of a call of this function were the same as the one in the second call, or like to put:
connection.disconnect();
it had no effect at all. Why and how can I resolve it?