黑莓HttpConnection.GET [英] blackberry HttpConnection.GET
问题描述
我们必须得到结果之前进行验证。
we must authenticate before getting the result.
我想送与 HttpConn.setRequestMethod的登录名和密码(HttpConnection.POST)
和检索 HttpConn.setRequestMethod XML文件(HttpConnection的不用彷徨)
用相同的HTTP客户端。
I want to send the login and password with HttpConn.setRequestMethod (HttpConnection.POST)
and retrieve the XML file with HttpConn.setRequestMethod (HttpConnection.GET)
with the same HTTP Client.
conn = (HttpConnection) new ConnectionFactory().getConnection(_url).getConnection();
URLEncodedPostData postData = null;
postData = new URLEncodedPostData("UTF-8", false);
postData.append("userName",_username);
postData.append("passWord", _password);
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LANGUAGE, "en-US");
conn.setRequestProperty(HttpProtocolConstants.HEADER_CACHE_CONTROL,"no-cache, no-store, no-transform");
// Specify the content type.
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, postData.getContentType());
byte [] postBytes = postData.getBytes();
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postBytes.length));
os = conn.openOutputStream();
os.write(postBytes);
os.flush();
os.close();
//GET XML file
conn.setRequestMethod(HttpConnection.GET);
conn.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Confirguration/CLDC-1.0");
if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
int total = 0;
int size = 1024;
char[] buffer = new char[size];
int len;
InputStreamReader isr = new InputStreamReader(conn.openInputStream(), "UTF-8");
while ((len = isr.read(buffer, 0, size)) > 0)
{
buff.append(buffer, 0, len);
total += len;
}
result = buff.toString();
} else {
result = "Error in connection" + conn.getResponseCode();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
conn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
在HttpConnection.POST工作得很好,但得不到任何(登录失败:认证参数不保存)
the HttpConnection.POST works very well but GET no (login failed: the authentication parameter does not saved)
推荐答案
在HttpConnection.GET请你需要在你喜欢的网址要追加属性:
In HttpConnection.GET request you need to append attribute in your url like:
String url="http://www.xyz?userName="+_username+"&password="+_password+"";
然后拿到的InputStream
and then get the InputStream
下面链接可能对你有所帮助。
below link may be helpful for you
<一个href=\"http://supportforums.blackberry.com/t5/Java-Development/Make-an-HTTP-Connection-to-get-a-Content-of-URL/td-p/95075\" rel=\"nofollow\">http://supportforums.blackberry.com/t5/Java-Development/Make-an-HTTP-Connection-to-get-a-Content-of-URL/td-p/95075
这篇关于黑莓HttpConnection.GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!