java - Cannot write output after reading input -


I am writing a program that connects servlet to a HttpURLConnection , but I get trapped The URL was checked

  Public zero connect (string method) throws exception {server = (HttpURLConnection) url.openConnection (); Server.setDoInput (true); Server.setDoOutput (true); Server.setUseCaches (wrong); Server.setRequestMethod (method); Server.setRequestProperty ("content-type", "app / XML"); Server.connect (); / * If (server.getResponseCode () == 200) {System.out.println ("The connection to the URL is OK:" + url); System.out.println ("------------------------------------------- ------- "); } Else System.out.println ("Connection Failed"); } * /   

I got the error:

java.net.ProtocolException: the output can not be written after reading the input.

If I check the URL in the comments with the code, but it works unfortunately without any complete work, I need to check the URL, so I think the problem getResponseCode method but I do not know

HTTP protocol is based on a Request-response Pattern: You send your request first and the server returns once the server returns Vaar replied, so you can not send any more material, that does not mean it will not be. (How do the server know about a response code before you want to send it?)

so when you call server.getResponseCode () , You effectively tell the server that your request has been exhausted and it can be processed if you want to send more data, then you have to start a new request.

By looking at your code, you want to see that the connection itself was successful, but there is no need for it: if there is no connection, successful T, a exception to Server.connect () . But the result of the connection effort is not the same as the HTTP response code, which always comes after processing it by server on all your inputs.

Comments