Blackberry HttpConnection and query string

元气小坏坏 提交于 2019-12-08 02:15:01

问题


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


来源:https://stackoverflow.com/questions/15248548/blackberry-httpconnection-and-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!