【问题标题】:blackberry HttpConnection.GET黑莓 HttpConnection.GET
【发布时间】:2011-07-12 11:04:47
【问题描述】:

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

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

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

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 运行良好但 GET 不成功(登录失败:身份验证参数未保存)

【问题讨论】:

    标签: http blackberry get httpconnection


    【解决方案1】:

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

    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

    【讨论】:

    • 感谢 Vivek Kumar Srivastava 的回复,但是当我在我的 url 中附加属性时,我收到“登录失败”而不是我需要的 XML 文件:(我们必须使用 POST 登录并 GET 到接收 XML 文件
    • 如果我直接在桌面浏览器上打开monSite/GET.asp?userName=vivek&passWord=1234,那么它也不起作用。如果此 url 不正确,则提供正确的 url。并且在您的代码中无需使用 conn.setRequestMethod(HttpConnection.GET);
    猜你喜欢
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 2011-09-04
    • 1970-01-01
    相关资源
    最近更新 更多