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.

4
  • 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? Commented Jun 5, 2012 at 11:38
  • 2
    There is a limit to the maximum length of URL. See: stackoverflow.com/questions/417142/… Commented Jun 5, 2012 at 11:38
  • @MaciejTrybiło ys i guess that's the issue,so is there some other way to do it?? Commented Jun 5, 2012 at 11:40
  • You can try doing it with a POST request. Here's an example: exampledepot.com/egs/java.net/post.html Sorry for a bitty answer! Commented Jun 5, 2012 at 11:44

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

how can i change that in the above URL?
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!

Comments

0

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

Comments

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

1 Comment

there is no method named url.getOutputStream() in the url

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.