java 模拟get请求

来源:互联网 发布:百鸟朝凤影评知乎 编辑:程序博客网 时间:2024/04/30 00:27

原码

public static void main(String[] args) {
  getHtmlCode("http://127.0.0.1/ttsys/doRebuild.do?username=%E6%96%B0%E7%A7%91%E6%97%97%E8%88%B0%E5%BA%97","utf-8");
 }

/**
  * <b>GET页面的内容</b>
  *
  * @param cityMsg
  * @return 返回HTML源代码
  */
 private  String getHtmlCode(String url,String code) {
  
  String html=null;
  try{
   //为了避免并发量采集,睡眠半秒钟。
   //Thread.sleep(500);
    String sCurrentLine; 

    StringBuffer sTotalString = new StringBuffer(""); 

    sCurrentLine=""; 

    java.io.InputStream l_urlStream; 
    //cityMsg=java.net.URLEncoder.encode(cityMsg, "UTF-8");
    java.net.URL l_url = new java.net.URL(url); 

    java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
    //l_connection.setRequestProperty("User-Agent","Mozilla/6.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
    int state=l_connection.getResponseCode();
    if(state==200){
    l_connection.connect(); 

    l_urlStream = l_connection.getInputStream(); 

    java.io.BufferedReader l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(l_urlStream,code)); 

    //得到返回的信息
    while ((sCurrentLine = l_reader.readLine()) != null) { 
     sTotalString.append(sCurrentLine);
     sTotalString.append("\n");
    } 
    l_reader.close();
    html = sTotalString.toString();
   
    }else{
     html=null;
    }
    l_connection.disconnect();
  }catch(Exception ex){
   log.error("读取网页异常:"+url, ex);
  }
  return html;
 }