JAVA中获取远程页面内容 和 xmlhttp远程处理

//BY ZGJ:获取远程页面内容
 public static String GetPageContent(String pageURL)
 {
  String pageContent="";
  BufferedReader in = null;
     InputStreamReader isr = null;
     InputStream is = null;
     PrintWriter pw=null;
     HttpURLConnection huc = null;
     try
     {
        URL url = new URL(pageURL);
        //创建 URL
        huc = (HttpURLConnection)url.openConnection();
        is = huc.getInputStream();
        isr = new InputStreamReader(is);
        in = new BufferedReader(isr);
        String line = null;
        while(((line = in.readLine()) != null))
        {
           if(line.length()==0)
               continue;
           pageContent+=line;
         }   
      }
      catch (Exception e)
      {
             System.err.println(e);
       }
       finally
       {  //无论如何都要关闭流
             try
             {is.close(); isr.close();in.close();huc.disconnect();pw.close();   
             }
             catch (Exception e) {}
      }
   return pageContent;
 }

xmlhttp远程处理

function Upload()
{
 var agt = navigator.userAgent.toLowerCase();
    var is_ie = (agt.indexOf('msie') != -1);
    var is_ie5 = (agt.indexOf('msie 5') != -1);
 function handle_do_search ()
 {
  if (xmlhttp.readyState == 4)//request completed
  {
   if (xmlhttp.status == 200)//request successful
   {
    var responseText = xmlhttp.responseText;
    if(responseText.indexOf("ok")==-1)
    {
     alert("上传出错,请查看错误信息!");
     document.getElementById("errInfo").innerText="错误信息:"+responseText;
    }
    else
    {
     alert("上传成功!");
     if("<%=fileCreate%>"=="")
      document.getElementById('xmlFrame').src="<%=request.getContextPath()%>"+"/out/UpDownData.xml";
    }
   }
   else
   {
    alert ("中心服务器无应答!");
   }
  }
 }
 var xmlhttp = null;
 if (is_ie)
 {
  var control = (is_ie5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
  try
  {
   xmlhttp = new ActiveXObject(control);
   xmlhttp.onreadystatechange = handle_do_search;
  } catch(e)
  {
   alert("请允许ActiveX controls执行!");
  }
 }
 else
 {
  xmlhttp = new XMLHttpRequest();
  xmlhttp.onload = handle_do_search;
  xmlhttp.onerror = handle_do_search;
 }
 xmlhttp.open("POST",uploadURL, false);
 xmlhttp.send(contentXML);
}

远程接收XMLHTTP处理:

request.setCharacterEncoding("UTF-8");
   BufferedReader in = request.getReader();
   String line="";
   String contentXML = "";
   while ((line = in.readLine()) != null)
   {
   //构造数据包
   contentXML = contentXML+line;
   }
   if(!contentXML.equals(""))
    resultMsg=JSPUtil.InsertData(contentXML);
   if(resultMsg.equals(""))
       out.println("ok");
      else
       out.println(resultMsg);

原文地址:https://www.cnblogs.com/willpower/p/1258174.html