开发者

blackberry server connection problem

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开发者_Python百科 anybody have any idea about this method then give me some hint about it.

enter code here


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜