zoukankan      html  css  js  c++  java
  • 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);

  • 相关阅读:
    python各种类型转换-int,str,char,float,ord,hex,oct等
    pandas快速入门
    python里面,将多个list列表合并成一个list列表
    对字符串进行切分的技巧
    Ubuntu 16.04 安装navicat (tar.gz)
    ubuntu 16.04 如何升级系统的scrapy旧版本(1.0.3)到最新版本
    ubuntu下,敲命令scrapy出现:0: UserWarning: You do not have a working installation of the service_identity module: 'cannot import name 'opentype''. Please install it from <https://pypi.python.org/pypi/servic
    Ubuntu下解压缩zip,tar,tar.gz,tar.bz2格式的文件
    简单的查看进程信息
    python正则表达式
  • 原文地址:https://www.cnblogs.com/willpower/p/1258174.html
Copyright © 2011-2022 走看看