无法执行请求-响应
我为黑莓中的客户端-服务器通信编写了以下代码,但它不起作用。
我正在通过 POST 发送数据。
使用黑莓模拟器进行测试时,我没有收到服务器的任何响应。
与Android & iPhone,我能够从同一服务器 url 和 url 获取响应具有相同的请求参数。
private void communicate() {
HttpConnection hc = null;
DataInputStream dis = null;
DataOutputStream dos = null;
StringBuffer messagebuffer = new StringBuffer();
try{
String input="firstname="+ fName.getText().trim()+
"&lastname=" + lName.getText().trim()+"&platform=blackberry";
String url = "http://127.0.0.1:80/index/login";
hc = (HttpConnection)
Connector.open(url, Connector.READ_WRITE); // Set the request method
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("User-Agent", "BlackBerry");
hc.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
hc.setRequestProperty("Content-Length",
Integer.toString(input.length()));
dos = hc.openDataOutputStream();
dos.write(input.getBytes());
dos.flush(); dos.close();
// Retrieve the response back from the servlet
dis = new DataInputStream(hc.openInputStream());
int ch;
// Check the Content-Length first
long len = hc.getLength();
if(len!=-1) {
for(int i = 0;i<len;i++)
if((ch = dis.read())!= -1)
messagebuffer.append((char)ch);
} else { // if the content-length is not available
while ((ch = dis.read()) != -1)
messagebuffer.append((char) ch);
}
dis.close();
}catch(Exception e){
e.printStackTrace();
}
}
请建议我是否需要对代码进行任何更改。
提前致谢。
CB
I wrote the following code for client - server communication in Blackberry but its not working.
I am sending data via POST.
I am not receiving any response from the server while testing using Blackberry simulator.
With Android & iPhone , I am able to get the response from the same server url & with the same request parameters.
private void communicate() {
HttpConnection hc = null;
DataInputStream dis = null;
DataOutputStream dos = null;
StringBuffer messagebuffer = new StringBuffer();
try{
String input="firstname="+ fName.getText().trim()+
"&lastname=" + lName.getText().trim()+"&platform=blackberry";
String url = "http://127.0.0.1:80/index/login";
hc = (HttpConnection)
Connector.open(url, Connector.READ_WRITE); // Set the request method
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("User-Agent", "BlackBerry");
hc.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
hc.setRequestProperty("Content-Length",
Integer.toString(input.length()));
dos = hc.openDataOutputStream();
dos.write(input.getBytes());
dos.flush(); dos.close();
// Retrieve the response back from the servlet
dis = new DataInputStream(hc.openInputStream());
int ch;
// Check the Content-Length first
long len = hc.getLength();
if(len!=-1) {
for(int i = 0;i<len;i++)
if((ch = dis.read())!= -1)
messagebuffer.append((char)ch);
} else { // if the content-length is not available
while ((ch = dis.read()) != -1)
messagebuffer.append((char) ch);
}
dis.close();
}catch(Exception e){
e.printStackTrace();
}
}
Kindly suggest if I need to make any changes in the code.
Thanks in advance.
CB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MDS 服务器不允许连接到 127.0.0.1 或 localhost。
The MDS server will not allow a connection to 127.0.0.1 or localhost.