URL,URLConnection,HttpURLConnection的简单应用

package com.cnse.urlDemo;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
 * URL,URLConnection,HttpURLConnection的简单
 * @author kxw
 */
public class UrlDemo {
    public static void main(String[] args) throws IOException {
//简单的url操作获取
        URL url=new URL("http://www.sohu.com/");
//获取url的io流操作
        InputStream inputStream=url.openStream();
        InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
//urlconnection获取输入流
        URLConnection urlConnection=url.openConnection();
        InputStream inputStream2=urlConnection.getInputStream();
//http通道httpurlconnection
        HttpURLConnection httpURLConnection=(HttpURLConnection)urlConnection;
        //设置从http读取写入
        httpURLConnection.setDoOutput(true); 
        httpURLConnection.setDoInput(true); 
        //设置请求方法
        httpURLConnection.setRequestMethod("POST"); 
        //http通道连接
        httpURLConnection.connect(); 
        InputStream inputStreamReader2=httpURLConnection.getInputStream();
    }
}

//可根据类型分别使用

你可能感兴趣的:(url,urlconnection)