URL对象的创建。
1、public URL(String spec);
URL urlbase=new URL("http://my.oschina.net/u/2308739/admin/new-blog.html");
2、public URL(URL context,String spec);
URL urlbase=new URL("http://my.oschina.net/u/2308739/admin/");
URL indexUrl=new URL(urlbase,"new-blog.html");
3、public URL(String protocol,String host,String file);
通过协议名、主机名、文件名构造一个URL对象。
new URL("http","http://my.oschina.net","/u/2308739/admin/new-blog.html");
4、public URL(String protocol,String host,int port,String file);
通过协议名、主机名、端口号,文件名构造一个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