2
POSTデータを送信した後、URLからHTML応答を読み取る必要があります。私はすでに以下の2つの機能を持っていますが、それらを組み合わせてPOSTデータを送信して応答を得ることはできません。BlackBerry経由でPOSTデータを送信した後にHTML応答を取得する
この関数は、標準のHTMLレスポンスを取得します。
public static String getDataFromUrl(String url) throws IOException {
System.out.println("------------FULL URL = " + url + " ------------");
StringBuffer b = new StringBuffer();
InputStream is = null;
HttpConnection c = null;
long len = 0;
int ch = 0;
c = (HttpConnection) Connector.open(url);
is = c.openInputStream();
len = c.getLength();
if (len != -1) {
// Read exactly Content-Length bytes
for (int i = 0; i < len; i++)
if ((ch = is.read()) != -1) {
b.append((char) ch);
}
} else {
// Read until the connection is closed.
while ((ch = is.read()) != -1) {
len = is.available();
b.append((char) ch);
}
}
is.close();
c.close();
return b.toString();
}
この関数は、URLにPOSTデータを送信します。
void postViaHttpConnection(String url) throws IOException {
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
int rc;
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-2.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, getResponseCode will flush
// Getting the response code will open the connection,
// send the request, and read the HTTP response headers.
// The headers are stored until requested.
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + rc);
}
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) {
int actual = 0;
int bytesread = 0;
byte[] data = new byte[len];
while ((bytesread != len) && (actual != -1)) {
actual = is.read(data, bytesread, len - bytesread);
bytesread += actual;
}
process(data);
} else {
int ch;
while ((ch = is.read()) != -1) {
process((byte) ch);
}
}
} catch (ClassCastException e) {
throw new IllegalArgumentException("Not an HTTP URL");
} finally {
if (is != null)
is.close();
if (os != null)
os.close();
if (c != null)
c.close();
}
}
だから私は私が得ることができるようにそれらを結合する方法を知っておく必要がありますPOSTデータを送信した後の応答。
(緊急!)わからない新技術ですか?正直言って、そのようなものは説明の中であなたを助けるつもりはありません。あなたが緊急の助けをしたい場合は、専門家を持って、あなたが支払うこと、立って。 – rwilliams
すみません、私のことはばかげていました。プログラムでこの問題を見落としてしまったので、すぐに修正する方法を見つける必要があります。 – Chro