Java基礎代碼庫:Java發起Http請求


import java.net.HttpURLConnection;
import java.net.URL;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class httpGet
{
    private URL url;
    private HttpURLConnection httpURLConn;
    public void myDoGet()
    {
        try
        {
            String temp = new String();
            url = new  URL("http://localhost:8080/myServlet/welcome");
            httpURLConn= (HttpURLConnection)url.openConnection();
            httpURLConn.setDoOutput(true);
            httpURLConn.setRequestMethod("GET");
            httpURLConn.setIfModifiedSince(999999999);
            httpURLConn.setRequestProperty("Referer", "http://localhost:80");
            httpURLConn.setRequestProperty("User-Agent", "test");
            httpURLConn.connect();
            InputStream in =httpURLConn.getInputStream();
            BufferedReader bd = new BufferedReader(new InputStreamReader(in));
            while((temp=bd.readLine())!=null)
            {
                System.out.println(temp);
            }            
        }
        catch (Exception e)
        {
            e.printStackTrace();
        } 
        finally
        {
            if(httpURLConn!=null)
            {
                httpURLConn.disconnect();
            }
        }
    }
    public static void main(String[] args)
    {
        httpGet hg = new httpGet();
        hg.myDoGet();
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM