当我附加查询字符串时,我在连接到 uri 时遇到了一些问题...我总是返回 400 http 代码...但是当我尝试浏览器时,相同的 url,一切都很顺利...
这就是我所拥有的:
String query = "q=hello";
byte[] queryBytes = query.getBytes();
在我的代码中的某个位置,我使用 queryBytes 打开一个 HttpConnection,如下所示:
String uri = "https://www.google.co.ve/search" + "?" + new String(queryBytes);
HttpConnection request = (HttpConnection) Connector.open(uri);
request.getResponseCode();
如果我不使用字节进行连接,一切都会正常工作:
String uri = "https://www.google.co.ve/search?q=hello";
提前致谢
最佳答案
当我尝试这个时,我得到http代码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();
}
关于rest - Blackberry HttpConnection 和查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15248548/