URL對象的建立。html
一、public URL(String spec);java
URL urlbase=new URL("http://my.oschina.net/u/2308739/admin/new-blog.html"); apache
二、public URL(URL context,String spec);服務器
URL urlbase=new URL("http://my.oschina.net/u/2308739/admin/");app
URL indexUrl=new URL(urlbase,"new-blog.html");url
三、public URL(String protocol,String host,String file);spa
經過協議名、主機名、文件名構造一個URL對象。.net
new URL("http","http://my.oschina.net","/u/2308739/admin/new-blog.html");code
四、public URL(String protocol,String host,int port,String file);orm
經過協議名、主機名、端口號,文件名構造一個URL對象。
new URL("http","http://my.oschina.net",80,"/u/2308739/admin/new-blog.html");
經過URL的openstream()方法,獲得Java.io.inputstream類的對象,從該輸入流中讀取URL地址的內容。
這個方法的定義是:
public final Inputstream openstream() throws IOException;
package com.yuan.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; public class TestUrl { public static void main(String[] args) { // TODO Auto-generated method stub try { //String accessurlString=URLEncoder.encode("http://www.ifeng.com/", "utf-8"); URL ifeng=new URL("http://www.ifeng.com/"); BufferedReader bReader=new BufferedReader(new InputStreamReader(ifeng.openStream()));//經過URL的openstrea//m()將數據轉換爲inputstream String inputlineString; while((inputlineString=bReader.readLine())!=null){ System.out.println(inputlineString); } bReader.close(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
經過對一個指定的URL之間創建一個鏈接,而後對該資源進行讀寫操做。
URLConnection重要的獲取鏈接的輸入/輸出流的方法。
Inputstream getInputstream()
Outputstream getOutputStream()
package com.yuan.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; public class TestUrl { public static void main(String[] args) { // TODO Auto-generated method stub try { //String accessurlString=URLEncoder.encode("http://www.ifeng.com/", "utf-8"); URL ifeng=new URL("http://www.ifeng.com/"); URLConnection urlConnection=ifeng.openConnection(); BufferedReader bReader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); String inputlineString; while((inputlineString=bReader.readLine())!=null){ System.out.println(inputlineString); } bReader.close(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.yuan.test; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.io.IOUtils; public class TestUrlConnetion { public static void main(String[] args){ try{ // Configure and open a connection to the site you will send the request URL url = new URL("http://wx.tclha.com/login.do"); URLConnection urlConnection = url.openConnection(); // 設置doOutput屬性爲true表示將使用此urlConnection寫入數據 urlConnection.setDoOutput(true); // 定義待寫入數據的內容類型,咱們設置爲application/x-www-form-urlencoded類型 urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded"); HttpURLConnection httpURLConnection = (HttpURLConnection)urlConnection;//httpurlconection 有能夠得到返回狀態值的方法。 /****httpurlconnetion一些配置屬性*httpURLConnection.setDoOutput(true); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("Accept-Charset", "utf-8"); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterData.length()));****/ // 獲得請求的輸出流對象 OutputStreamWriter out = new OutputStreamWriter(httpURLConnection.getOutputStream()); // 把數據寫入請求的Body out.write("userName=yuanw&passWord=121212"); out.flush(); out.close(); System.out.println("ResponseCode:"+httpURLConnection.getResponseCode()); if (httpURLConnection.getResponseCode() >= 300) { System.out.println("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode()); } // 從服務器讀取響應 InputStream inputStream = urlConnection.getInputStream(); String encoding = urlConnection.getContentEncoding(); System.out.println("encoding:"+encoding); String body = IOUtils.toString(inputStream, "utf-8"); System.out.println(body); }catch(IOException e){ Logger.getLogger(TestUrlConnetion.class.getName()).log(Level.SEVERE, null, e); } } }
更多內容參考博文:
參考博文:http://www.cnblogs.com/nick-huang/p/3859353.html