• 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();
        }
    }
  • 相关阅读:
    hdu 2639 Bone Collector II
    文件打包bundle
    iOS UITextField垂直居中
    Mac删除废纸篓中的单一文件和文件夹
    Swift 初见
    关于 Swift
    NSString和NSDate的转换
    iOS开发之iOS7设置状态栏字体颜色
    在当前界面中隐藏状态栏
    适合所有测试人员读的书籍
  • 原文地址:https://www.cnblogs.com/todsong/p/2637002.html
Copyright © 2020-2023  润新知