连接在一段时间后关闭

时间:2011-12-30 07:05:05

标签: web-services blackberry blackberry-eclipse-plugin blackberry-jde j2mepolish

我使用以下代码进行互联网连接

HttpConnection httpConn = null;
DataOutputStream dataOS = null;
redemptionUrl = redemptionUrl+ ";deviceside=true";
httpConn = (HttpConnection) Connector.open(redemptionUrl);

httpConn.setRequestProperty("User-Agent",
"Profile/MIDP-1.0, Configuration/CLDC-1.0");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("Content-Language", "en-US");
httpConn.setRequestMethod(HttpConnection.POST);
dataOS = (DataOutputStream) httpConn.openDataOutputStream();
dataOS.flush();
dataOS.close();
DataInputStream dataIS = (DataInputStream) httpConn
.openDataInputStream();
int ch;
sb = new StringBuffer();
System.out.println("%%%% Me here 4 sb is ::" + sb.toString());
while ((ch = dataIS.read()) != -1) {
sb.append((char) ch);
}
// Respeonse
// -------------------------------------------------------------
System.out.println("sb.toString()::" + sb.toString());
String responseData = sb.toString();
dataIS.close();
httpConn.close();

连接断开一段时间后。什么是错的,任何人都可以提供帮助

1 个答案:

答案 0 :(得分:0)

public class HttpPostRetriver extends Thread{
private String _url;
private StringBuffer _postData;
private byte[] _data=new byte[512];
private HttpConnection _httpConnection;
private OutputStream os;
private InputStream is;

public HttpPostRetriver(String url) {
    _url = url+UrlSuffix.updataConnectionSuffix();
    try {
      _httpConnection = (HttpConnection)Connector.open(_url);
    } catch(Exception e) {
    }
}

public String postData() {
     try {
        _httpConnection.setRequestMethod(HttpConnection.POST);
        _httpConnection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
        _httpConnection.setRequestProperty("Content-Language", "en-US");
        _httpConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        os = _httpConnection.openOutputStream();

        int rc = _httpConnection.getResponseCode();
        if(rc == HttpConnection.HTTP_OK) {
              is = _httpConnection.openInputStream();
              is.read(_data);
        } else {
          _data = null;
        }
   } catch(Exception e) {
       //exception
   }
   return (new String(_data));
}
}

试试这段代码.........