blackberry server connection problem

别来无恙 提交于 2019-12-12 01:53:56

问题


I want to send data to the server by using URLEncodedPost Class. I am getting error while i am trying to call the POST method. so if anybody have any idea about this method then give me some hint about it.

enter code here

回答1:


You are not posted any code to know which error you are getting any way, the following code is an example of Http Post method

HttpConnection connection = (HttpConnection) Connector.open("url", Connector.READ_WRITE);
connection.setRequestMethod(HttpConnection.POST); 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);
encPostData.append("username","your username");
encPostData.append("password","ur password");
byte[] postData = encPostData.toString().getBytes("UTF-8");
connection.setRequestProperty("Content-Length", String.valueOf(postData.length));
OutputStream os = connection.openOutputStream();
os.write(postData);
os.flush(); 
int responseCode = connection.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK)
{
 System.out.println("Unexpected response code: "+ responseCode);
connection.close();
return;
}
String contentType = connection.getHeaderField("Content-type");
baos = new ByteArrayOutputStream();
InputStream responseData = connection.openInputStream();
byte[] buffer = new byte[10000];
int bytesRead = responseData.read(buffer);
while (bytesRead > 0)
{
baos.write(buffer, 0, bytesRead);
bytesRead = responseData.read(buffer);
}
baos.close();
connection.close();
System.out.println("Server response"+new String(baos.toByteArray()));


来源:https://stackoverflow.com/questions/7471756/blackberry-server-connection-problem

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