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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值