URLConnection

URLJDK8
这里写图片描述
   URLConnection是个抽象类,它表示指向URL指定资源的的活动连接,可以检查服务器的发送的首部,并且处理response信息。它有两个直接子类分别是HttpURLConnection和JarURLConnection。

   另外一个重要的类是URL,通常URL可以通过传给构造器一个String类型的参数来生成一个指向特定地址的URL实例。URL类和URLConnection类的区别是,URLConnection类提供了对HTTP首部的访问,可以配置request信息,除了读取服务器数据外,还可以向服务器写入数据。

     每个 HttpURLConnection 实例都可用于生成单个请求,但是其他实例可以透明地共享连接到 HTTP 服务器的基础网络。请求后在 HttpURLConnection 的 InputStream 或 OutputStream 上调用 close() 方法可以释放与此实例关联的网络资源,但对共享的持久连接没有任何影响。如果在调用 disconnect() 时持久连接空闲,则可能关闭基础套接字。


补充: http = (HttpURLConnection) url.openConnection();

1.设置request中的head信息
http.setRequestProperty(“key值”, “value值”);

2.在response head中根据key值相应的value
http.getHeaderFields().get(“key值”);

3.doInput属性:为trues时表明从URL连接中读取数据;默认值为true;
这里写图片描述

4.doOutput属性:为true时表明可以将数据写到URL连接中(当用POST方法请求资源时候,需要手动设置属性值true);默认为false;
这里写图片描述

5:getInputStream()方法:返回从这个打开的连接读取的输入流,即读取服务器response回来的信息。如果在数据可用之前读取超时过期,则可以从返回的输入流中读取一个SocketTimeoutException;

6:getOutputStream()方法:获取该连接的输出流,用POST方法请求时,需要用到该方法,将POST 的内容写到连接中。


示例代码1:demo

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
//java实现源码的获取
public class WebPageSource {
    public static void main(String args[]){    
        URL url;
        int responsecode;
        HttpURLConnection urlConnection;
        BufferedReader reader;
        String line;
        try{
            //生成一个URL对象,要获取源代码的网页地址为:http://www.sina.com.cn
            url=new URL("http://www.baidu.com.cn");
            //打开URL
            urlConnection = (HttpURLConnection)url.openConnection();
            //获取服务器响应代码
            responsecode=urlConnection.getResponseCode();
            if(responsecode==200){
                //得到输入流,即获得了网页的内容 
                reader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
                while((line=reader.readLine())!=null){
                    System.out.println(line);
                }
            }
            else{
                System.out.println("获取不到网页的源码,服务器响应代码为:"+responsecode);
            }
        }
        catch(Exception e){
            System.out.println("获取不到网页的源码,出现异常:"+e);
        }
    }
}

示例代码2:通过.get方法访问

package com.mark.TestOther;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/***
 * 1.构造一个URL对象
 * 2.调用这个这个URL对象的openConnection()--->获得一个URLConnection对象
 * 3.配置URLConnection
 * 4.读取首部信息
 * 5.获得输入流--->读取数据
 * 6.获得输出流--->写入数据
 * 7.关闭连接
 * @author YIMA
 *
 */
public class HttpURLConnectionTest {
	private static String TARGET_URL ="https://www.baidu.com/?tn=99055797_hao_pg";
	
	public static void main(String[] args) throws IOException{
		HttpURLConnectionTest test = new HttpURLConnectionTest();
		System.out.println(test.getResponse());
	}

	private String getResponse() throws IOException {
		URL url = null;
		HttpURLConnection http = null;
		BufferedReader bufferedReader =null;
		InputStream inputStream = null;
		StringBuffer sTotalString = new StringBuffer();
		try{
			//1.构造一个URL对象
			url = new URL(TARGET_URL);
			
			//2.调用这个这个URL对象的openConnection()
			http = (HttpURLConnection) url.openConnection();
			
			//3设置请求的方式
			http.setRequestMethod("GET");
			
			//5.获得输入流--->读取数据
			inputStream = http.getInputStream();
			bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
			
			// 6.获得输出流--->写入数据
			String sCurrentLine = null;
			while((sCurrentLine = bufferedReader.readLine())!= null){
				sTotalString.append(sCurrentLine);
				sTotalString.append("\r\n");
			}
			
			return sTotalString.toString();
		}catch(MalformedURLException e){
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			bufferedReader.close();
			inputStream.close();
			http.disconnect();
		}
		return null;
	}
}

示例代码3:通过get方法访问:(设置代理,获取cookie,设置request信息)

package com.mark.TestOther;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
/***
 * 1.构造一个URL对象
 * 2.调用这个这个URL对象的openConnection()--->获得一个URLConnection对象
 * 3.配置URLConnection
 * 4.读取首部信息
 * 5.获得输入流--->读取数据
 * 6.获得输出流--->写入数据
 * 7.关闭连接
 * 8http.setRequestProperty("key","value"):设值
 * 9http.getHeaderFields().get("key"):取值
 * @author YIMA
 *
 */
public class HttpURLConnectionTest {
	private static String TARGET_URL ="****URL of Resquset***";
	private static String user_agent_value="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; GTB7.5; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727)";
	//是否需要保存cookie信息
	private boolean enableCookie = true;
	private Map<String,String> cookieMap ;

	//是否设置代理
	private boolean needProxy = false;
	private final String hostname ="";//代理IP
	private final int port = 8080;//代理端口
	
	public int time_out_value = 3 * 60 * 1000;//等待超时时间

	public static void main(String[] args) throws IOException{
		HttpURLConnectionTest test = new HttpURLConnectionTest();
		System.out.println(test.getResponse());
	}

	private String getResponse() throws IOException {
		URL url = null;
		HttpURLConnection http = null;
		BufferedReader bufferedReader =null;
		InputStream inputStream = null;
		StringBuffer sTotalString = new StringBuffer();
		try{
			//构造一个URL对象
			url = new URL(TARGET_URL);

			//调用这个这个URL对象的openConnection()
			if(needProxy){
				InetSocketAddress inetSocketAddress = new InetSocketAddress(hostname, port);
				Proxy proxy = new Proxy(Proxy.Type.HTTP,inetSocketAddress);
				http = (HttpURLConnection) url.openConnection(proxy);
			}else{
				http = (HttpURLConnection) url.openConnection();
			}


			//设置请求的方式和相关信息
			http.setRequestMethod("GET");
			http.setConnectTimeout(time_out_value);
			http.setReadTimeout(time_out_value);
			http.setDoInput(false);//以后就可以使用conn.getInputStream().read();默认值是true
			//设置头部的请求参数
			http.setRequestProperty("User-Agent", user_agent_value);

			//添加cookie信息
			SetCookieToRequestIfNecessary(http);

			//获得输入流--->读取数据
			inputStream = http.getInputStream();
			bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

			//获得输出流--->写入数据
			String sCurrentLine = null;
			while((sCurrentLine = bufferedReader.readLine())!= null){
				sTotalString.append(sCurrentLine);
				sTotalString.append("\r\n");
			}

			//是否需要保存cookie
			if(enableCookie){
				parseCookieProperties(http);
			}

			return sTotalString.toString();
		}catch(MalformedURLException e){
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			bufferedReader.close();
			inputStream.close();
			http.disconnect();
		}
		return null;
	}


	//设置cookie值
	private void SetCookieToRequestIfNecessary(HttpURLConnection http) {
		enableCookie = false;
		if(!enableCookie || cookieMap.size()==0){
			return;
		}
		String cookieValue = buildCookieContent();
		http.setRequestProperty("Cookie", cookieValue);
	}

	//构造cookieValue值
	private String buildCookieContent() {
		StringBuffer cookieStr = new StringBuffer();
		for(String key:cookieMap.keySet()){
			cookieStr.append(key).append("=").append(cookieMap.get(key));
		}
		return cookieStr.toString();
	}

	//取出response信息中的cookie信息保存到一个Map中
	//这里只保存cookie中的name 和 value 的信息,而path,demain等没有保存
	private void parseCookieProperties(HttpURLConnection http) {
		//如果服务器返回的信息中没有Set-Cookie,就结束
		if(http.getHeaderFields().get("Set-Cookie").size()==0){
			return;
		}

		if(cookieMap == null){
			cookieMap = new HashMap<String, String>();
		}

		for(String cookie:http.getHeaderFields().get("Set-Cookie")){
			System.out.println(cookie);
			if(cookie == "" || !cookie.contains("=")){
				continue;
			}

			String cookieName = cookie.substring(0, cookie.indexOf("="));
			String cookieValue = cookie.substring(cookie.indexOf("=") + 1);

			if(cookie.contains(";")){
				cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.indexOf(";"));
			}

			cookieMap.put(cookieName, cookieValue);
		}
	}
}



post方法

package com.mark.TestOther;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class HttpURLConnectionPost {
	private static final String TARGET_URL ="http://www.hmm21.com/ebiz/schedule/getSailingSchedules.jsp";
	private static String queryparameter;// post 请求的参数;
	private static String user_agent_value="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; GTB7.5; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727)";

	//是否设置代理
	private boolean needProxy = false;
	private final String hostname ="";//代理IP
	private final int port = 8080;//代理端口
	
	public int time_out_value = 3 * 60 * 1000;//等待超时时间
	
	public static void main(String[] args) throws IOException {
		HttpURLConnectionPost test = new HttpURLConnectionPost();
		Map<String,String> paramMap = constructPostMap();
		queryparameter = constructPostParams(paramMap);
		System.out.println(test.getResponse());
	}

	private static String constructPostParams(Map<String, String> paramMap) {
		StringBuffer sb = new StringBuffer();
		for(String key:paramMap.keySet()){
			if(key == null){
				return null;
			}
			
			if(!(sb.toString() != ""&&sb.toString().contains("?"))){
				sb.append("?");
			}else{
				sb.append("&");
			}
			sb.append(key);
			sb.append("=");
			sb.append(paramMap.get(key));
		}
		return sb.toString().substring(1);
	}

	private static Map<String, String> constructPostMap() {
		Map<String,String> paramMap = new HashMap<String, String>();
		paramMap.put("key1", "value1");
		paramMap.put("key2", "value2");
		paramMap.put("key3", "value3");
		paramMap.put("key4", "value4");
		paramMap.put("key5", "value5");
		return paramMap;
	}

	private String getResponse() throws IOException {
		URL url = null;
		HttpURLConnection http = null;
		OutputStreamWriter out = null;
		BufferedReader bufferedReader =null;
		InputStream inputStream = null;
		StringBuffer sTotalString = new StringBuffer();
		
		try{
			//构造一个URL对象
			url = new URL(TARGET_URL);

			//调用这个这个URL对象的openConnection()
			if(needProxy){
				InetSocketAddress inetSocketAddress = new InetSocketAddress(hostname, port);
				Proxy proxy = new Proxy(Proxy.Type.HTTP,inetSocketAddress);
				http = (HttpURLConnection) url.openConnection(proxy);
			}else{
				http = (HttpURLConnection) url.openConnection();
			}


			//设置请求的方式和相关信息
			http.setRequestMethod("POST");
			http.setConnectTimeout(time_out_value);//连接超时 单位毫秒
			http.setReadTimeout(time_out_value);//读取超时 单温毫秒
			
			//发送POST请求必须设置如下两行
			http.setDoInput(true);//以后就可以使用conn.getInputStream().read();默认值是true
			http.setDoOutput(true);//以后就可以使用conn.getOutputStream().write() 
			
			//设置头部的请求参数
			http.setRequestProperty("User-Agent", user_agent_value);

			// 获取URLConnection对象对应的输出流
			out = new OutputStreamWriter(http.getOutputStream(),"UTF-8");
			if(queryparameter != null){
				out.write(queryparameter);// 发送请求参数
			}
			//out.flush();
			out.close();
			
			//获得输入流--->读取数据
			inputStream = http.getInputStream();
			bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

			//获得输出流--->写入数据
			String sCurrentLine = null;
			while((sCurrentLine = bufferedReader.readLine())!= null){
				sTotalString.append(sCurrentLine);
				sTotalString.append("\r\n");
			}

			return sTotalString.toString();
		}catch(MalformedURLException e){
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			bufferedReader.close();
			inputStream.close();
			http.disconnect();
		}
		return null;
	}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值