URLConnection

E

Erik

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.
 
A

Arne Vajhøj

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
 
E

EJP

Erik said:
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.
 
E

Erik

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();
}
}
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top