URLConnection

Discussion in 'Java' started by Erik, Jan 9, 2010.

  1. Erik

    Erik Guest

    I do:
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    and get an error : already connected
    on the doOutput.

    openConnection is not supposed to connect , is it ?
    ..connect is....

    I'm on localhost, Tomcat 5.port 8080, Netbeans 6.8.
    Erik, Jan 9, 2010
    #1
    1. Advertising

  2. Erik

    Arne Vajhøj Guest

    On 09-01-2010 16:28, Erik wrote:
    > I do:
    > URLConnection conn = url.openConnection();
    > conn.setDoOutput(true);
    >
    > and get an error : already connected
    > on the doOutput.
    >
    > openConnection is not supposed to connect , is it ?
    > .connect is....
    >
    > I'm on localhost, Tomcat 5.port 8080, Netbeans 6.8.


    Can we get a bit more context?

    I have used:

    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("POST");
    con.setDoOutput(true);
    OutputStream os = con.getOutputStream();
    os.write(b);
    con.connect();

    many times.

    Maybe GET vs POST ??

    Arne
    Arne Vajhøj, Jan 9, 2010
    #2
    1. Advertising

  3. Erik

    EJP Guest

    Erik wrote:
    > and get an error : already connected
    > on the doOutput.


    You must have already done some I/O on the connection, e.g. a read, or
    get the response code.
    EJP, Jan 10, 2010
    #3
  4. Erik

    Roedy Green Guest

    On Sat, 09 Jan 2010 22:28:18 +0100, Erik <> wrote,
    quoted or indirectly quoted someone who said :

    > URLConnection conn = url.openConnection();
    >conn.setDoOutput(true);
    >
    >and get an error : already connected
    >on the doOutput.


    for sample code see http://mindprod.com/jgloss/http.html

    download source at http://mindprod.com/products1.html#HTTP
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    There is no end to what can be accomplished if you don’t care who gets the credit.
    ~ Art Rennison
    Roedy Green, Jan 10, 2010
    #4
  5. Erik

    Erik Guest

    GET-POST seems to be an issue here indeed. However,after adaptation
    according to your suggestions, I still get a 500-error, from the W3C
    site as well as my localhost.

    Further below is the relevant piece of source code.

    Firefox has this plug-in to invesxtigate HTTP data sent. this gives
    the following when using the url :
    http://jigsaw.w3.org/css-validator/validator?text=h2{margin-left:23px;}

    =========================================
    http://jigsaw.w3.org/css-validator/validator

    POST /css-validator/validator HTTP/1.1
    Host: jigsaw.w3.org
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
    rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)
    Accept:
    text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: en-us,en;q=0.5
    Accept-Encoding: gzip,deflate
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive: 300
    Connection: keep-alive
    Referer: http://jigsaw.w3.org/css-validator/
    Content-Type: multipart/form-data;
    boundary=---------------------------114782935826962
    Content-Length: 666
    -----------------------------114782935826962
    Content-Disposition: form-data; name="text"

    h2{margin-left:23px;}
    -----------------------------114782935826962
    Content-Disposition: form-data; name="profile"

    css21
    -----------------------------114782935826962
    Content-Disposition: form-data; name="usermedium"

    all
    -----------------------------114782935826962
    Content-Disposition: form-data; name="type"

    none
    -----------------------------114782935826962
    Content-Disposition: form-data; name="warning"

    1
    -----------------------------114782935826962
    Content-Disposition: form-data; name="lang"

    en
    -----------------------------114782935826962--

    =======================================
    the relevant piece of my source is this:

    public class URLConnectionTest
    {
    public static void main(String[] args)
    {
    try
    {
    String urlName =
    "http://jigsaw.w3.org/css-validator/validator";

    URL url = new URL(urlName);
    HttpURLConnection con = (HttpURLConnection)
    url.openConnection();
    con.setRequestMethod("POST");
    con.setDoOutput(true);
    PrintWriter os = new PrintWriter(con.getOutputStream());

    os.print("text="+ URLEncoder.encode("h2{margin-left:34px;}", "UTF-8")
    + "&" );
    os.print("profile"+ URLEncoder.encode("css21", "UTF-8") +
    "&" );
    os.print("usermedium="+ URLEncoder.encode("all", "UTF-8")
    + "&" );
    os.print("type="+ URLEncoder.encode("none", "UTF-8") + "&"
    );
    os.print("warning="+ URLEncoder.encode("1", "UTF-8") + "&"
    );
    os.print("lang="+ URLEncoder.encode("en", "UTF-8") );

    os.close();
    // print header fields

    int n = 1;
    String key;
    while ((key = con.getHeaderFieldKey(n)) != null)
    {
    String value = con.getHeaderField(n);
    System.out.println(key + ": " + value);
    n++;
    }

    // print convenience functions

    System.out.println("----------");
    System.out.println("getContentType: "
    + con.getContentType());
    System.out.println("getContentLength: "
    + con.getContentLength());
    System.out.println("getContentEncoding: "
    + con.getContentEncoding());
    System.out.println("getDate: "
    + con.getDate());
    System.out.println("getExpiration: "
    + con.getExpiration());
    System.out.println("getLastModifed: "
    + con.getLastModified());
    System.out.println("----------");


    BufferedReader in = new BufferedReader(new
    InputStreamReader(con.getInputStream()));

    // print first ten lines of contents

    String line;
    n = 1;
    while ((line = in.readLine()) != null && n <= 100)
    {
    System.out.println(line);
    n++;
    }
    if (line != null) System.out.println(". . .");

    }
    catch (IOException exception)
    {
    exception.printStackTrace();
    }
    }


    On Sat, 09 Jan 2010 18:07:26 -0500, Arne Vajhøj <>
    wrote:

    >On 09-01-2010 16:28, Erik wrote:
    >> I do:
    >> URLConnection conn = url.openConnection();
    >> conn.setDoOutput(true);
    >>
    >> and get an error : already connected
    >> on the doOutput.
    >>
    >> openConnection is not supposed to connect , is it ?
    >> .connect is....
    >>
    >> I'm on localhost, Tomcat 5.port 8080, Netbeans 6.8.

    >
    >Can we get a bit more context?
    >
    >I have used:
    >
    >HttpURLConnection con = (HttpURLConnection) url.openConnection();
    >con.setRequestMethod("POST");
    >con.setDoOutput(true);
    >OutputStream os = con.getOutputStream();
    >os.write(b);
    >con.connect();
    >
    >many times.
    >
    >Maybe GET vs POST ??
    >
    >Arne
    >
    Erik, Jan 10, 2010
    #5
  6. Erik

    Roedy Green Guest

    On Sun, 10 Jan 2010 12:10:52 +0100, Erik <> wrote,
    quoted or indirectly quoted someone who said :

    >GET-POST seems to be an issue here indeed. However,after adaptation
    >according to your suggestions, I still get a 500-error, from the W3C
    >site as well as my localhost.


    See http://mindprod.com/jgloss/jgloss/http.html
    and copy the code verbatim for POST.

    I notice for example that you left out the urlc.connect();
    That is probably not the problem, but it is indicative you may have
    othero differences.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    There is no end to what can be accomplished if you don’t care who gets the credit.
    ~ Art Rennison
    Roedy Green, Jan 11, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kristoffel

    URLConnection and proxy

    Kristoffel, Jul 27, 2003, in forum: Java
    Replies:
    0
    Views:
    1,562
    Kristoffel
    Jul 27, 2003
  2. Brock Jones
    Replies:
    0
    Views:
    2,624
    Brock Jones
    Sep 4, 2003
  3. Bronwyn Howard
    Replies:
    0
    Views:
    6,088
    Bronwyn Howard
    Oct 1, 2003
  4. dave
    Replies:
    0
    Views:
    829
  5. Replies:
    2
    Views:
    3,569
Loading...

Share This Page