首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >黑莓HttpConnection.GET

黑莓HttpConnection.GET
EN

Stack Overflow用户
提问于 2011-07-12 19:04:47
回答 1查看 1.3K关注 0票数 0

网址:http://monSite/GET.asp

在得到结果之前,我们必须进行身份验证。

我想用HttpConn.setRequestMethod (HttpConnection.POST)发送登录名和密码,并用相同的HTTP客户端用HttpConn.setRequestMethod (HttpConnection.GET)检索XML文件。

代码语言:javascript
复制
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运行得很好,但没有得到(登录失败:身份验证参数未保存)

EN

回答 1

Stack Overflow用户

发布于 2011-07-12 21:42:35

在HttpConnection.GET请求中,您需要在url中添加如下属性:

代码语言:javascript
复制
String url="http://www.xyz?userName="+_username+"&password="+_password+"";

然后拿到InputStream

下面的链接可能对你有帮助

http://supportforums.blackberry.com/t5/Java-Development/Make-an-HTTP-Connection-to-get-a-Content-of-URL/td-p/95075

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6663221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档