Skip to main content

Http proxy issue

4 replies [Last post]
tusharj9
Offline
Joined: 2005-09-22
Points: 0

Hi,
I able to successfully connect to any http server inside local network using phoneme MIDlets. But if I try to access external http server from behind the proxy, I'm getting error "111".
I have compiled pcsl library for using bsd/generic sockets, and testing phoneme inside scratchbox. Also tried to set http_proxy environment variable, but that too didn't help.

Any code change require to handle proxy at native layer ?

Regards,
Tushar

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jannewmarch
Offline
Joined: 2006-06-01
Points: 0

Setting the property com.sun.midp.io.http.proxy only works with certain proxies configured in certain ways. The usual way of talking to proxy is to open a TCP connection and then issue commands such as "GET url HTTP/1.1" where the url is the resource you really want to access. The proxy is expected to forward the request to the desired host and relay the response.

MIDP uses proxy tunneling instead (expired internet draft "Tunneling TCP based protocols through Web proxy servers"). It sends "CONNECT remote-host" to the proxy which should then open a stream to the remote host. If successful, the proxy returns "200 Okay" and the client can then send HTTP requests (or anything) over the stream. It does this so it can support HTTPS.

Many proxies refuse to handle CONNECT requests. e.g. my work proxy will only handle them for port 443, the normal HTTPS port. Solutions like "use another proxy" or "reconfigure the proxy" are not an option in our work environment.

Solution? Write your own HTTP client which can talk to proxies using the normal mechanism. It's not TOO hard to write a basic HTTP client, but it starts to get messy if you need to handle HTTP redirects, chunked documents, etc, etc. All the code to do that is already in MIDP and it wouldn't take much to support proxies as in the JDK. Sigh :-(

Here is some naive code, use at your own risk:

public InputStream getInputStreamFromURL(String url) {
InputStream is = null;
try {
// Are we using a proxy?
httpProxy = System.getProperty("com.sun.midp.io.http.proxy");
if (httpProxy == null) {
// No proxy, connect directly
HttpConnection conn = (HttpConnection)
Connector.open("http://" + url);
return conn.openInputStream();

} else {
/*
* we need to talk over TCP to the proxy and send HTTP requests to it
*/
SocketConnection conn = (SocketConnection)
Connector.open("socket://" + httpProxy);

OutputStream os = conn.openOutputStream();
DataOutputStream out = new DataOutputStream(os);

// send an HTTP 1.0 request so we get a simple reply
String request = "GET " + url + " HTTP/1.0\r\n\r\n";

byte[] b = request.getBytes();
for (int n = 0; n < b.length; n++)
out.writeByte(b[n]);

is = conn.openInputStream();
int ch;
// Discard response headers
for (;;) {
String line;
line = readLine(is);
if (line.equals("")) {
break;
}
}
}

return is;
} catch(Exception e) {
e.printStackTrace();
return null;
}
}

public String readLine(InputStream is) {
int c;
StringBuffer stringbuffer = new StringBuffer();
stringbuffer.setLength(0);

for (;;) {
try {
c = is.read();
if (c < 0) {
return null;
}
if (c == '\r') {
continue;
}
} catch (IOException ioe) {
return null;
}
if (c == '\n') {
break;
}
stringbuffer.append((char)c);
}
return stringbuffer.toString();
}

amak
Offline
Joined: 2009-07-04
Points: 0

Hi,

You may be interested in this database of open source HTTP proxies written in Java.

http://proxies.xhaus.com/java/

There's also a list of open source HTTP proxies written in python.

http://proxies.xhaus.com/python/

Regards,

Alan.

tusharj9
Offline
Joined: 2005-09-22
Points: 0

Got this proxy issue resolved.
If you are behind the proxy then you need to modify midp/src/configuration/configuration_xml/share/properties.xml and set com.sun.midp.io.http.proxy to the address of your proxy.
Recompile and then your midlet should be able to connect to servers of outside network.

Regards,
Tushar

babaroga84
Offline
Joined: 2008-01-12
Points: 0

Hello
I have sort of a same problem. I have a MIDlet application on one side and Apache proxy server on the other side. When I debug my application thorough Sun emulator everything works fine. When I port my app to my mobile device in 1 of 7 (proximately) attempts I successfully obtain a connection through Apache proxy. Basically, sometimes it works sometimes it doesn't. When it doesn't work I receive NullPointerException. Now, I see you managed to solve your problem. Could you tell me where to find that properties.xml file you referred to. It is really confusing because sometimes on my mobile device app works and sometimes it doesn't and that's driving me crazy!

Thanks for you reply in advance,
Sava