zoukankan      html  css  js  c++  java
  • HttpURLConnection(二)

    package com.cmy.urlcon;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.Reader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;
    
    public class HttpURLConnectionDemo {
    
        public static void main(String[] args) throws Exception {
            // crwyUrl();
            //getURLData1();
            //getURLData2();
            
            urlSupportType();
        }
    
        public static void crwyUrl() throws Exception {
    
            URL url = new URL(
                    "http://blog.csdn.net/a9529lty/article/details/6454145");
            Object obj = url.getContent();
            System.out.println(url.getContent());
            System.out.println(obj.getClass().getName());
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
    
            Reader read = new InputStreamReader(url.openStream());
            URLConnection urlConnection = url.openConnection();
            InputStream in = urlConnection.getInputStream();
            int c;
            while ((c = in.read()) != -1) {
                System.out.print((char) c);
            }
            url.getAuthority();
    
        }
    
        public static void getURLData1() throws Exception {
            URL url = new URL(
                    "http://lavasoft.blog.51cto.com/attachment/200811/200811271227767778082.jpg");
            Object obj = url.getContent();
            System.out.println("getURLData1获取指定资源: " + obj.getClass().getName());
        }
    
        public static void getURLData2() throws Exception {
            URL url = new URL(
                    "http://blog.csdn.net/chenzheng_java/article/details/6248090");
            URLConnection con = url.openConnection();
            InputStream in = con.getInputStream();
            int c;
            while ((c = in.read()) != -1) {
                System.out.print((char) c);
            }
            in.close();
        }
    
        public static void getURLData3() throws Exception {
            URL url = new URL(
                    "http://blog.csdn.net/chenzheng_java/article/details/6248090");
            InputStream in = url.openStream();
            int c;
            while ((c = in.read()) != -1) {
                System.out.print((char) c);
            }
            in.close();
        }
    
        public static void urlSupportType() throws Exception {
            String host = "www.java2s.com";
            String file = "/index.html";
    
            String[] schemes = { "http", "https", "ftp", "mailto", "telnet",
                    "file", "ldap", "gopher", "jdbc", "rmi", "jndi", "jar", "doc",
                    "netdoc", "nfs", "verbatim", "finger", "daytime",
                    "systemresource" };
    
            for (int i = 0; i < schemes.length; i++) {
                try {
                    URL u = new URL(schemes[i], host, file);
                    System.out.println(schemes + " is supported
    ");
                } catch (Exception ex) {
                    System.out.println(schemes + " is not supported
    ");
                }
            }
        }
        
        /**
         * HttpURLConnection getMethod
         * @return
         * @throws Exception
         */
        public static String urlDoGet() throws Exception{
            URL url = new URL("http://localhost:8080/OneHttpServer/");
            HttpURLConnection con = (HttpURLConnection)url.openConnection();
            
            con.setRequestProperty("Accept-Charset","utf-8");
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            con.setConnectTimeout(3000);
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false);
            String cookie0 = con.getHeaderField("Set-Cookie");
            
            InputStream inputStream = null;
            InputStreamReader inputStreamReader = null;
            BufferedReader bfReader = null;
            StringBuffer sb = new StringBuffer();
            String readLine = null;
            
            if(con.getResponseCode() >= 300){
                throw new Exception("HTTP Request is not success, Response code is " + con.getResponseCode());
            }
            
            try {
                inputStream = con.getInputStream();
                inputStreamReader = new InputStreamReader(inputStream);
                bfReader = new BufferedReader(inputStreamReader);
                while((readLine = bfReader.readLine()) != null){
                    sb.append(readLine);
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                bfReader.close();
                inputStreamReader.close();
                inputStream.close();
            }
            
            return sb.toString();
        }
        
        /**
         * HttpURLConnection postMethod
         * @return
         * @throws Exception
         */
        public static String urlDoPost() throws Exception{
            URL url = new URL("http://localhost:8080/OneHttpServer/");
            HttpURLConnection con = (HttpURLConnection)url.openConnection();
            String parameterData = "username=nickhuang&blog=http://www.cnblogs.com/nick-huang/";
            
            con.setRequestProperty("Accept-Charset","utf-8");
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            con.setConnectTimeout(3000);
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false);
            String cookie0 = con.getHeaderField("Set-Cookie");
            
            OutputStream outputStream = null;
            OutputStreamWriter outputStreamWriter = null;
            InputStream inputStream = null;
            InputStreamReader inputStreamReader = null;
            BufferedReader bfReader = null;
            StringBuffer sb = new StringBuffer();
            String readLine = null;
            
            try {
                outputStream = con.getOutputStream();
                outputStreamWriter = new OutputStreamWriter(outputStream);
                outputStreamWriter.write(parameterData.toString());
                outputStreamWriter.flush();
                
            } catch (Exception e) {
                // TODO: handle exception
            }
            
            if(con.getResponseCode() >= 300){
                throw new Exception("HTTP Request is not success, Response code is " + con.getResponseCode());
            }
            
            try {
                inputStream = con.getInputStream();
                inputStreamReader = new InputStreamReader(inputStream);
                bfReader = new BufferedReader(inputStreamReader);
                while((readLine = bfReader.readLine()) != null){
                    sb.append(readLine);
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                outputStreamWriter.close();
                outputStream.close();
                bfReader.close();
                inputStreamReader.close();
                inputStream.close();
            }
            
            return sb.toString();
        }
    }
  • 相关阅读:
    7、python数据类型之集合set
    python基本数据类型练习
    matplotlib
    numpy常用函数
    pillow包
    keras-tensorflow版本对应
    python-激活和切换运行环境
    端口监控
    numpy
    低风险创业笔记
  • 原文地址:https://www.cnblogs.com/tingbogiu/p/6274492.html
Copyright © 2011-2022 走看看