日期:2014-05-20 浏览次数:20827 次
public HTTPResponse post(String host, int port, boolean isKeepAlive)
{
HTTPResponse httpRes = new HTTPResponse();
setHost(host);
setConnection((isKeepAlive == true) ? HTTP.KEEP_ALIVE : HTTP.CLOSE);
boolean isHeaderRequest = isHeadRequest();
OutputStream out = null;
InputStream in = null;
try {
if (postSocket == null){
// Thanks for Hao Hu
postSocket = new Socket();
postSocket.connect(new InetSocketAddress(host, port), HTTPServer.DEFAULT_TIMEOUT);
}
out = postSocket.getOutputStream();
PrintStream pout = new PrintStream(out);
pout.print(getHeader());
pout.print(HTTP.CRLF);
boolean isChunkedRequest = isChunked();
String content = getContentString();
int contentLength = 0;
if (content != null)
contentLength = content.length();
if (0 < contentLength) {
if (isChunkedRequest == true) {
// Thanks for Lee Peik Feng <pflee@users.sourceforge.net> (07/07/05)
String chunSizeBuf = Long.toHexString(contentLength);
pout.print(chunSizeBuf);
pout.print(HTTP.CRLF);
}
pout.print(content);
if (isChunkedRequest == true)
pout.print(HTTP.CRLF);
}
if (isChunkedRequest == true) {
pout.print("0");
pout.print(HTTP.CRLF);
}
pout.flush();
in = postSocket.getInputStream();
httpRes.set(in, isHeaderRequest);
} catch (SocketException e) {
httpRes.setStatusCode(HTTPStatus.INTERNAL_SERVER_ERROR);
Debug.warning(e);
} catch (IOException e) {
//Socket create but without connection
//TODO Blacklistening the device
httpRes.setStatusCode(HTTPStatus.INTERNAL_SERVER_ERROR);
Debug.warning(e);
} finally {
if (isKeepAlive == false) {
try {
in.close();
} catch (Exception e) {};
if (in != null)
try {
out.close();
} catch (Exception e) {};
if (out != null)
try {
postSocket.close(); // 警告在这里
} catch (Exception e) {};
postSocket = null;
}
}
return httpRes;
}