0

If the temp string is very large I get java.io.IOException: Error writing to server at getInputStream

String tmp  = js.deepSerialize(taskEx);
URL url = new URL("http://"
                    + "localhost"
                    + ":"
                    + "8080"
                    + "/Myproject/TestServletUpdated?command=startTask&taskeId=" +taskId + "'&jsonInput={\"result\":"
                    + URLEncoder.encode(tmp) + "}"); 

                    URLConnection conn = url.openConnection();
                     InputStream is = conn.getInputStream();

Why is that? This call goes to the servlet mentioned in the URL.

Harinder
  • 11,776
  • 16
  • 70
  • 126
  • 2
    Well given that it's local, you should be able to see what happened at the server side. Check for exceptions. Additionally, there may well be more information than the message you're showing here - is there an inner exception? – Jon Skeet Jun 05 '12 at 11:38
  • 2
    There is a limit to the maximum length of URL. See: http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url – Maciej Trybiło Jun 05 '12 at 11:38
  • @MaciejTrybiło ys i guess that's the issue,so is there some other way to do it?? – Harinder Jun 05 '12 at 11:40
  • You can try doing it with a POST request. Here's an example: http://www.exampledepot.com/egs/java.net/post.html Sorry for a bitty answer! – Maciej Trybiło Jun 05 '12 at 11:44

4 Answers4

3

Use HTTP POST method instead of putting all the data in the URL for GET method. There is an upper limit for the length of the URL, so you need to use POST method if you want to send arbitrary length data.

You may want to modify the URL to http://localhost:8080/Myproject/TestServletUpdated, and put the rest

command = "startTask&taskeId=" + taskId + "'&jsonInput={\"result\":" + URLEncoder.encode(tmp) + "}"

in the body of the POST request.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
2

I think you might have a "too long url", the maximum number of characters are 2000 (see this SO post for more info). GET requests are not made to handle such long data input.

You can, if you can change the servlet code also, change it into a POST instead of a GET request (as you have today). The client code would look pretty simular:

public static void main(String[] args) throws IOException {

    URL url = new URL("http", "localhost:8080", "/Myproject/TestServletUpdated");

    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write("command=startTask" +
             "&taskeId=" +taskId +
             "&jsonInput={\"result\":" + URLEncoder.encode(tmp) + "}");
    wr.flush();


    .... handle the answer ...
}

I didn't see it first but it seems like you have a single quote character in your request string.

...sk&taskeId=" + taskId + "'&jso.....
                            ^

try removing it, it might help you!

Community
  • 1
  • 1
dacwe
  • 43,066
  • 12
  • 116
  • 140
0

It could be because the request is being sent as a GET which has a limitation of a very few characters. When the limit exceeds you get an IOException. Convert that to POST and it should work.

For POST

URLConnection conn = url.openConnection().
OutputStream writer = conn.getOutputSteam();
writer.write("yourString".toBytes());

Remove the temp string from the url that you are passing. Move the "command" string to the "yourString".toBytes() section in the code above

Varun Achar
  • 14,781
  • 7
  • 57
  • 74
0

getInputStream() is used to read data. Use getOutputStream()

drzymala
  • 2,009
  • 20
  • 26