Java codepublic class XmlTransfer{
private String urlAddr;
private String xmlStr;
HttpURLConnection urlCon = null;
public XmlTransfer(String _urlAddr,String _xmlStr) {
this.urlAddr = _urlAddr;
this.xmlStr = _xmlStr;
}
public InputStream get() throws Exception
{
if(urlCon==null){urlCon=getUrlConnection();}
if(urlCon==null){throw new Exception("连接失败");}
PrintWriter out = new PrintWriter(urlCon.getOutputStream());
urlCon.disconnect();
InputStream fin1 = urlCon.getInputStream();
return fin1;
}
private HttpURLConnection getUrlConnection(){
try{
URL url = new URL(urlAddr);
URLConnection conn = url.openConnection();
urlCon = (HttpURLConnection)conn;
urlCon.setRequestProperty("Content-type", "text/html;charset=utf-8");
urlCon.setDoOutput(true);
urlCon.setRequestMethod("GET");
urlCon.setUseCaches(false);
}
catch (MalformedURLException mex) {
mex.printStackTrace();
}
catch (ProtocolException pex) {
pex.printStackTrace();
}
catch (IOException iex) {
iex.printStackTrace();
}
return urlCon;
}
public String getHttp( String strURL ){
XmlTransfer xt=new XmlTransfer(strURL,"");
StringBuffer sb = new StringBuffer();
try{
InputStream is = xt.get();
byte[] b = new byte[1024];
int iCount = 0;
while ((iCount = is.read(b)) > 0) {
sb.append(new String(b, 0, iCount));
}
}catch(Exception e){
sb.append("An error occurs in XmlTransfer.getHttp\n");
sb.append(e.getMessage());
}
return (sb.toString());
}
}