13.06.2012, 15:30
Hi all, I have a problem with HttpConnection. Here is the code:
And it's request headers are:
The problem is that I can't set own request headers, it sends User-Agent as Profile/MIDP-2.1 Configuration/CLDC-1.1 UNTRUSTED/1.0, instead of Profile/MIDP-2.1 Configuration/CLDC-1.1.
Код:
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.io.*; import java.io.*; public class Midlet extends MIDlet implements CommandListener { StringItem si = new StringItem("",""); Form f = new Form("Internetas",new Item[] { si }); Command exit = new Command("Iљeiti",Command.EXIT,0); Command ok = new Command("OK",Command.OK,0); public void commandAction(Command c, Displayable d) { if(c == exit) { notifyDestroyed(); } if(c == ok) { HttpConnection conn = null; InputStream is = null; try { conn = (HttpConnection) Connector.open("http://localhost/a/"); conn.setRequestMethod(HttpConnection.GET); conn.setRequestProperty("User-Agent","Profile/MIDP-2.1 Configuration/CLDC-1.1"); int rc = conn.getResponseCode(); String str; if(rc != HttpConnection.HTTP_OK) { str = new String(rc + " " + conn.getResponseMessage()); } else { is = conn.openInputStream(); int len = (int) conn.getLength(); if(len != -1) { byte id[] = new byte[len]; is.read(id); str = new String(id); } else { ByteArrayOutputStream bs = new ByteArrayOutputStream(); int ch; while((ch = is.read()) != -1) { bs.write(ch); } str = new String(bs.toByteArray()); bs.close(); } } si.setText(str); } catch(IOException e) { throw new IllegalArgumentException(e.getMessage()); } finally { if(is != null) { try { is.close(); } catch(Exception e) { throw new IllegalArgumentException(e.getMessage()); } } if(conn != null) { try { conn.close(); } catch(Exception e) { throw new IllegalArgumentException(e.getMessage()); } } } } } public void startApp() { f.addCommand(exit); f.addCommand(ok); f.setCommandListener(this); Display.getDisplay(this).setCurrent(f); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }
Код:
User-Agent: Profile/MIDP-2.1 Configuration/CLDC-1.1 UNTRUSTED/1.0 x-network-type: null Content-Length: 0 Host: localhost Accept: */*