Blackberry HttpConnection and query string

2019-05-28 14:21发布

问题:

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...

This is what I have:

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

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();

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

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

Thanks in advance

回答1:

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();
}