Как в J2ME отправить POST данные по HTTP?

Модератор: Absurd

Deady
Сообщения: 193
Зарегистрирован: 17 фев 2004, 13:13
Откуда: Москва
Контактная информация:

16 мар 2005, 14:55

Example using POST with HttpConnection

Post a request with some headers and content to the server and process the headers and content.

Connector.open is used to open url and a HttpConnection is returned. The request method is set to POST and request headers set. A simple command is written and flushed. The HTTP headers are read and processed. If the length is available, it is used to read the data in bulk. From the HttpConnection the InputStream is opened. It is used to read every character until end of file (-1). If an exception is thrown the connection and stream is closed.


void postViaHttpConnection(String url) throws IOException {
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;

try {
c = (HttpConnection)Connector.open(url);

// Set the request method and headers
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("If-Modified-Since",
"29 Oct 1999 19:43:31 GMT");
c.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-US");

// Getting the output stream may flush the headers
os = c.openOutputStream();
os.write("LIST games\n".getBytes());
os.flush(); // Optional, openInputStream will flush

// Opening the InputStream will open the connection
// and read the HTTP headers. They are stored until
// requested.
is = c.openInputStream();

// Get the ContentType
String type = c.getType();
processType(type);

// Get the length and process the data
int len = (int)c.getLength();
if (len > 0) {
byte[] data = new byte[len];
int actual = is.read(data);
process(data);
} else {
int ch;
while ((ch = is.read()) != -1) {
process((byte)ch);
}
}
} finally {
if (is != null)
is.close();
if (os != null)
os.close();
if (c != null)
c.close();
}
}



взято из javadoc для MIDP 2.0
Deady
Сообщения: 193
Зарегистрирован: 17 фев 2004, 13:13
Откуда: Москва
Контактная информация:

16 мар 2005, 15:00

задавая параметры в строке, ты автоматически предполагаешь использование get.
post параметры передаются в теле запроса.

можно например в перловом скрипте обрабатывать и строку запроса, и тело запроса. у сервлетов это все делается автоматом. надо доки читать, как получить доступ ко всем параметрам (если это вообще возможно).
bulda
Сообщения: 31
Зарегистрирован: 17 фев 2004, 12:59
Контактная информация:

16 мар 2005, 17:01

Serverside скрипты как правило умеют вытаскивать параметры из тела цикла POST. Надо что бы параметры записывались в стандартном виде. Что то типа

login=blablabla&password=xxx
mobius
Сообщения: 151
Зарегистрирован: 25 янв 2005, 18:42
Откуда: Минск
Контактная информация:

17 мар 2005, 12:25

прикол был в чём, не нужно os.flush() делать, он туда всякий мусор сыпал, вот и ошибки были, всем спасибо!
Всё об отдыхе на http://chugaga.com
Ответить