日期:2014-05-20 浏览次数:20728 次
public String sendAndReceive(String srcUrl) { HttpURLConnection connection = null; InputStream in = null; try { URL url = new URL(srcUrl); connection = (HttpURLConnection)url.openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/MSIE"); int contentLen = connection.getContentLength(); in = connection.getInputStream(); if(contentLen <= 0) { contentLen = bufLen; int offset = 0; do { int len = in.read(buffer, offset, contentLen - offset); if(len <= 0) break; offset += len; } while(true); contentLen = offset; } else { if(contentLen > bufLen) { buffer = new byte[contentLen]; bufLen = contentLen; } int offset = 0; do { int len = in.read(buffer, offset, contentLen - offset); if(len <= 0) break; offset += len; } while(true); contentLen = offset; } String resMsg = connection.getHeaderField(0); if(resMsg.toLowerCase().indexOf("ok") < 0) { return null; } else { connection.disconnect(); in.close(); String content = new String(buffer, 0, contentLen); return content; } } catch(Exception e) { if(connection != null) connection.disconnect(); if(in != null) try { in.close(); } catch(IOException ioexception) { } e.printStackTrace(); return null; } }