黑莓的HttpConnection和查询字符串

黑莓的HttpConnection和查询字符串

本文介绍了黑莓的HttpConnection和查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有连接到一个URI一些麻烦,当我追加查询字符串...我总是回来HTTP 400 code ...但是当我尝试浏览器相同的URL,一切都顺利。 ..

I've been having some trouble connecting to a uri when I append a query string... I always get back 400 http code... however when I try the browser, same url, everything goes smooth...

这是我有:

String query = "q=hello";
byte[] queryBytes = query.getBytes();

在某处我的code我打开使用queryBytes像这样的HttpConnection的:

Somewhere in my code I open an HttpConnection using the queryBytes like this:

String uri = "https://www.google.co.ve/search" + "?" + new String(queryBytes);
HttpConnection request = (HttpConnection) Connector.open(uri);
request.getResponseCode();

如果我不使用我的连接字节everyting工作正常:

If I don't use bytes for my connection everyting works fine:

String uri = "https://www.google.co.ve/search?q=hello";

在此先感谢

推荐答案

当我尝试这一点,IAM越来越HTTP code 200。

When i try this, iam getting http code 200.

try {
    String httpURL = "https://www.google.co.ve/search?q=hello";
    HttpConnection httpConn;
    httpConn = (HttpConnection) Connector.open(httpURL);
    httpConn.setRequestMethod(HttpConnection.GET);
    DataOutputStream _outStream = new DataOutputStream(httpConn.openDataOutputStream());
    byte[] request_body = httpURL.getBytes();
    for (int i = 0; i < request_body.length; i++) {
        _outStream.writeByte(request_body[i]);
    }
    DataInputStream _inputStream = new DataInputStream(
    httpConn.openInputStream());
    StringBuffer _responseMessage = new StringBuffer();
    int ch;
    while ((ch = _inputStream.read()) != -1) {
        _responseMessage.append((char) ch);
    }
    String res = (_responseMessage.toString());
    String responce = res.trim();
    httpConn.close();

    Dialog.alert(responce);

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

这篇关于黑莓的HttpConnection和查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 06:31