Java obtains web page source code and simulates browser request

Source: Internet
Author: User

Directly attach the code, copy runs directly

Package com.sh.browser;import java.io.bufferedreader;import java.io.fileoutputstream;import  java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import  java.io.outputstreamwriter;import java.io.printwriter;import java.net.url;import  java.net.urlconnection;public class getpagecontent { /**  *  @param   Args  */ public static void main (String[] args)  {  try  {   //http://api.map.baidu.com/geocoder/v2/?ak=kqipaektt6xaorkmbmlvbzze&callback= renderreverse&location=31.249161,121.487899&output=json&pois=1&qq-pf-to=pcqq.c2c     url u1=new url ("http://api.map.baidu.com/location/ip?ak=E4805d16520de693a3fe707cdc962045 &ip=202.198.16.3&coor=bd09ll ");            url  url = new url ("Http://api.Map.baidu.com/location/ip?ak=e4805d16520de693a3fe707cdc962045&ip=183.56.167.29&coor=bd09ll ");         urlconnection conn = url.openconnection ();     conn.setdooutput (True);    inputstream in = null;     in = url.openstream ();     string content = pipe (In, "Utf-8");         system.out.println (content);           } catch  (exception e)  {    E.printstacktrace ();   } } static string pipe (inputstream in,string  CharSet)  throws IOException {        StringBuffer  S = new stringbuffer ();         if (charset==null| | "". Equals (CharSet)) {         charset= "Utf-8";        }         String rLine = null;         bufferedreader breader = new bufferedreader (New InputStreamReader ( In,charset));        printwriter pw = null;           fileoutputstream fo = new fileoutputstream (".. /index.html ");   outputstreamwriter writer = new outputstreamwriter (fo, " Utf-8 ");   pw = new printwriter (writer);         while  (  (Rline = breader.readline ())  != null)  {             String tmp_rLine = rLine;              int str_len = tmp_rline.length ();             if  (str_len > 0)  {               s.append (Tmp_rline);               pw.println (Tmp_rline);               pw.flush ();             }            tmp_ rline = null;       }         in.close ();         pw.close ();         return s.tostring ();  }}

Java obtains web page source code and simulates browser request

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.