码迷,mamicode.com
首页 > 编程语言 > 详细

java 发起 HTTP请求

时间:2014-12-16 00:46:42      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   color   os   sp   java   on   

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

java 发起 HTTP请求

标签:style   http   io   ar   color   os   sp   java   on   

原文地址:http://www.cnblogs.com/xigua1hao/p/4166154.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!