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(); } }
//可根據類型分別使用
java