HttpURLConnection through a Proxy.


J

junk1

Im having problems with the following code...

URL url;
Properties sysProps = System.getProperties();
sysProps.put( "proxySet", "true" );
sysProps.put( "proxyHost", "192.168.211.17");
sysProps.put( "proxyPort", "80" );

Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication("userid",
"password".toCharArray()));
}};
Authenticator.setDefault(authenticator);

try {

url = new
URL("http://www.realtime-traffic.info/rtt/loadMap.do?gridcode=GR05&textonly=true");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.connect();
System.out.println("con.getResponseCode():[" + con.getResponseCode()
+ "]");
System.out.println("con:[" + con + "]");
System.out.println("con.getContentLength():[" +
con.getContentLength() + "]");
System.out.println("con.getContent():[" + con.getContent() + "]");
InputStream inputStream = con.getInputStream();
StringBuffer trafficData = new StringBuffer();
for (int n = 0; n < con.getContentLength(); n++) {
trafficData.append((char) inputStream.read());
}
System.out.println("trafficData:[" + trafficData + "]");
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

.....it gives me a response code of 200. What does 200 mean? And why
wont it connect to the URL?

It works fine If I point it at localhost.

I copied the Authenticator bit from another post, is it the correct way
to do proxy authentication?

Thanks

David Bevan
http://www.davidbevan.co.uk
 
Ad

Advertisements

R

Rob Skedgell

....it gives me a response code of 200. What does 200 mean? And why
wont it connect to the URL?

HttpURLConnection.HTTP_OK == 200

From RFC2616 "HTTP/1.1" (<http://www.ietf.org/rfc/rfc2616.txt>):

10.2.1 200 OK

The request has succeeded. The information returned with the response
is dependent on the method used in the request, for example:

GET an entity corresponding to the requested resource is sent in
the response;

(GET is the default method for an HTTP request)

I'm not sure why it's not working since the server appears to have
reported success to the client.
 
A

Alexey Shevchenko

I've no time to delve into.......
Please compare my code

private String loadPage( URL parURL ) throws Exception
{
long lTime = System.currentTimeMillis();
String http=parURL.toString();
// Create a connection.
HttpURLConnection urlConnection =
(HttpURLConnection)parURL.openConnection();
urlConnection.setRequestMethod( "GET" );
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestProperty("USER-AGENT",userAgent );
//
urlConnection.setRequestProperty("Content-type","application/x-www-form-urlenCcoded");
urlConnection.setAllowUserInteraction( true );

urlConnection.connect();

InputStream in =
((HttpURLConnection)urlConnection).getInputStream();

StringWriter sw = new StringWriter();
 
A

Alexey Shevchenko

Ah! I forgot

userAgent = mainProperties.getProperty( "User.agent", "Mozilla/2.02Gold
(WinNT; I)" );

It is emulation of your client type



Alexey Shevchenko said:
I've no time to delve into.......
Please compare my code

private String loadPage( URL parURL ) throws Exception
{
long lTime = System.currentTimeMillis();
String http=parURL.toString();
// Create a connection.
HttpURLConnection urlConnection =
(HttpURLConnection)parURL.openConnection();
urlConnection.setRequestMethod( "GET" );
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestProperty("USER-AGENT",userAgent );
//
urlConnection.setRequestProperty("Content-type","application/x-www-form-urlenCcoded");
urlConnection.setAllowUserInteraction( true );

urlConnection.connect();

InputStream in =
((HttpURLConnection)urlConnection).getInputStream();

StringWriter sw = new StringWriter();
.
.
.
}

It is work code.







Im having problems with the following code...

URL url;
Properties sysProps = System.getProperties();
sysProps.put( "proxySet", "true" );
sysProps.put( "proxyHost", "192.168.211.17");
sysProps.put( "proxyPort", "80" );

Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication("userid",
"password".toCharArray()));
}};
Authenticator.setDefault(authenticator);

try {

url = new
URL("http://www.realtime-traffic.info/rtt/loadMap.do?gridcode=GR05&textonly=true");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.connect();
System.out.println("con.getResponseCode():[" + con.getResponseCode()
+ "]");
System.out.println("con:[" + con + "]");
System.out.println("con.getContentLength():[" +
con.getContentLength() + "]");
System.out.println("con.getContent():[" + con.getContent() + "]");
InputStream inputStream = con.getInputStream();
StringBuffer trafficData = new StringBuffer();
for (int n = 0; n < con.getContentLength(); n++) {
trafficData.append((char) inputStream.read());
}
System.out.println("trafficData:[" + trafficData + "]");
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

....it gives me a response code of 200. What does 200 mean? And why
wont it connect to the URL?

It works fine If I point it at localhost.

I copied the Authenticator bit from another post, is it the correct way
to do proxy authentication?

Thanks

David Bevan
http://www.davidbevan.co.uk
 
E

EJP

sysProps.put( "proxySet", "true" );
sysProps.put( "proxyHost", "192.168.211.17");
sysProps.put( "proxyPort", "80" );

These property names are obsolete. proxySet does nothing, and for
proxyHost and proxyPort you should really use http.proxyHost and
http.proxyPort respectively.
 
Ad

Advertisements

J

junk1

Ive now realised that I dont need a proxy, and some more tests on local
URLS reveal that it works for things ending .html, but not for anything
else, ie .jsp

Does this suggests that its something to do with the mime type? If so
then how do I set this so that it copes with things that dont end in
..html?

Thanks

David Bevan
http://www.davidbevan.co.uk
 

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

Similar Threads


Top