十二、网络编程

十二、网络编程
    ServerSocket,Socket,Jsoup,HttpClient,URLConnection
    ServerSocket:服务器Socket端口
                ①实例化ServerSocket对象设定指定端口
                ②调用accept()方法等待客户端连接
    Socket:客户端端口对象
                ①实例化Socket设置连接的地址和端口
                ②通过OutputStream与InputStream进行系统的交互
    Jsoup:一个解析很优秀的工具,抓包简单但不能抓跳转。
                ①Jsoup.connect(urlStr)连接即可
                ②通过返回Document进行解析
    HttpClient:一个抓包很优秀的工具
                CloseableHttpClient     核心类
                HttpPost     post方式连接
                CloseableHttpResponse   回调对象
                RequestConfig  设置请求头
                BasicNameValuePair 设置post信息
                HttpEntity 回调的实体类
    实例:
         SocketServer.java :
package net.itaem.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * 服务器端口
 * @author sen
 * @version 1.0,2014年8月28日
 */
public class SocketServer {
	
	/**
	 * <String,Socket> String 为客户端id(数据库id) ,Socket为端口 
	 */
	public static Map<String,Socket> CLIENTS = new HashMap<String,Socket>() ;

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		
		/*
		 * 1.初始化服务端端口对象
		 */
		ServerSocket server = null ;
		try {
			server = new ServerSocket(8189) ;	//创建端口
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/**
		 * 等待并获取客户端链接
		 * 启动线程处理请求(此处最好使用线程池)
		 */
		while(true){
			Socket client = null ;
			try {
				client = server.accept() ;	//等待客户端连接
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			new Thread(new ServerDriver(client)).start();
			
		}
		
	}
	
	/**
	 * 
	 * 接收客户端信息
	 * @author sen
	 * @param clientId
	 * @return
	 * @throws IOException
	 */
	public static String in(Socket socket) throws IOException{
		BufferedReader bufIn = new BufferedReader(new InputStreamReader(socket.getInputStream())) ;
		return bufIn.readLine() ;
	}
	
	/**
	 * 
	 * 发送信息到客服端
	 * @author sen
	 * @param clientId
	 * @param message
	 * @throws IOException
	 */
	public static void out(Socket socket,String message) throws IOException{
		OutputStream outStream = socket.getOutputStream() ;
		PrintWriter out = new PrintWriter(outStream,true) ;
		out.print(message);
	}
	
	
}
/**
 * 
 * 实现线程驱动的类
 * @author sen
 * @version 1.0,2014年8月28日
 */
class ServerDriver implements Runnable {
	
	private Socket socket = null;
	
	public ServerDriver(Socket socket) {
		this.socket = socket;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			/*
			 * 不断的接收信息进行相应的处理
			 */
			while(true){
				String choose = SocketServer.in(socket) ; //接收信息
				if(choose.equals("login")){
					//...动作
					//回调信息
					SocketServer.out(socket,"message");
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

         SocketClient.java :
package net.itaem.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;

/**
 * 
 * 客户端端口
 * @author sen
 * @version 1.0,2014年8月28日
 */
public class SocketClient {
	
	private static SocketChannel CHANNEL = null ;

	public SocketClient(SocketChannel channel){
		SocketClient.CHANNEL = channel ;
	}
	
	public static void main(String[] args) throws IOException {
		SocketChannel channel = SocketChannel.open(new InetSocketAddress("localhost",8189)) ;
		new SocketClient(channel).start() ;
	}
	
	/**
	 * 
	 * 发送信息到服务器
	 * @author sen
	 * @param channel
	 * @param message
	 * @throws IOException
	 */
	public static void out(SocketChannel channel,String message) throws IOException{
		OutputStream outStream = channel.socket().getOutputStream() ;
		PrintWriter out = new PrintWriter(outStream,true) ;
		out.print(message);
	}
	
	/**
	 * 
	 * 接收服务器信息
	 * @author sen
	 * @param channel
	 * @return
	 * @throws IOException
	 */
	public static String in(SocketChannel channel) throws IOException{
		BufferedReader bufIn = new BufferedReader(new InputStreamReader(channel.socket().getInputStream())) ;
		return bufIn.readLine() ;
	}
	
	public void start(){
		/*
		 * 1.Init Window 初始化窗口并打开窗口
		 */
		
		/*
		 * 2.运行线程
		 */
		new Thread(new ReceiveDriver(CHANNEL)).start(); 
	}
}

//客户端总接收信息
class ReceiveDriver implements Runnable{
	
	private SocketChannel channel = null ;
	
	public ReceiveDriver(SocketChannel channel){
		this.channel = channel ;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			String choose = new String();            //功能选择变量
			try {
				choose = SocketClient.in(channel) ;    //接收服务器信息进行相应处理
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(choose==null || "".equals(choose) ||"null".equals(choose)){
				break ;
			}
			switch(Integer.parseInt(choose)){
			
			}
		}
	}
}


JsoupUtils.java  :
package net.itaem.net;

import java.io.IOException;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

/**
 * 
 * Jsoup工具包
 * @author sen
 * @version 1.0,2014年8月28日
 */
public class JsoupUtils {
	
	/**
	 * 
	 * 1.GET请求
	 * @author sen
	 * @param urlStr url地址
	 * @return
	 * @throws IOException
	 */
	public static Document doGet(String urlStr) throws IOException {
		
		return getConnection(urlStr).get() ;
	}
	
	/**
	 * 
	 * 2.POST请求
	 * @author sen
	 * @param urlStr url地址
	 * @param params 参数
	 * @return
	 * @throws IOException
	 */
	public static Document doPost(String urlStr,Map<String,String> params) throws IOException{
		return getConnection(urlStr)
				.data(params)
				.post() ;
	}
	
	
	
	/**
	 * 
	 * 获取并设置好的请求头
	 * @author sen
	 * @param urlStr url地址
	 * @return
	 */
	private static Connection getConnection(String urlStr){
		Connection conn = Jsoup.connect(urlStr)
				.header("Accept", "text/html, application/xhtml+xml, */*")
				.header("Accept-Encoding", "gzip, deflate")
				.header("Accept-Language", "zh-CN")
				.header("Connection", "Keep-Alive")
				.header("Cookie", "ASP.NET_SessionId=jktzqa554z3cptyb305ztzyw")
				.header("Accept-Encoding", "gzip, deflate")
				.header("User-Agent","Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)") ;
				
		return conn ;
	}
}


HttpClientUtils.java :
package net.itaem.net;


import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;


import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;




/**
 * 
 * HttpClient工具包
 * @author sen
 * @version 1.0,2014年8月28日
 */
public class HttpClientUtils {
	
	public static void main(String[] args) throws Exception {
		System.out.println(HttpClientUtils.doGet("http://org.job.csdn.net/Job/DetailsNew?ID=88111"));
	}
	
	/**
	 * 
	 * GET方式
	 * @author sen
	 * @param urlStr	url地址
	 * @return
	 * @throws Exception
	 */
	public static String doGet(String urlStr) throws Exception{
		/*
		 * 初始化对象 
		 */
		//HttpClient核心类
		CloseableHttpClient httpClient = HttpClients.createDefault();
		//地址以及回调对象
		HttpGet httpGet = new HttpGet(urlStr);
		CloseableHttpResponse response ;
		//代理
		//HttpHost proxy = new HttpHost("www.java2000.net", 80);
		
		/*
		 * 设置超时
		 */
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(2000)
				.setConnectTimeout(2000)
		//		.setProxy(proxy)
				.build();//设置请求和传输超时时间
		httpGet.setConfig(requestConfig);
		
		/*
		 * 执行
		 */
		String html = null ;
		response = httpClient.execute(httpGet);
		
		HttpEntity httpEntity;
		httpEntity = response.getEntity();
        if(httpEntity != null){
            html = readHtmlContentFromEntity(httpEntity);
        }
		
		/*
		 * 返回结果
		 */
		//获取头信息
		//Header[] headers=response.getHeaders("Location");
		return html ;
	}
	
	/**
	 * 
	 * POST方式
	 * @author sen
	 * @param urlStr	url地址
	 * @param params	参数集合
	 * @return
	 * @throws Exception
	 */
	public static String doPost(String urlStr,Map<String,String> params) throws Exception {
		/*
		 * 初始化对象 
		 */
		//HttpClient核心类
		CloseableHttpClient httpClient = HttpClients.createDefault();
		//地址以及回调对象
		HttpPost httpPost = new HttpPost(urlStr);
		CloseableHttpResponse response ;
		//代理
		//HttpHost proxy = new HttpHost("127.0.0.1");
		/*
		 * 设置超时
		 */
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(2000)
				.setConnectTimeout(2000)
		//		.setProxy(proxy) 
				.build();//设置请求和传输超时时间
		httpPost.setConfig(requestConfig);


		
		/*
		 * 设置参数
		 */
		//添加参数
		if(params!=null && params.size()>0){
			List<NameValuePair> fromParams = new ArrayList<NameValuePair>() ;
			for(Map.Entry<String, String> entry : params.entrySet()){
				fromParams.add(new BasicNameValuePair(entry.getKey(),entry.getValue())) ;
			}
			//参数编码
			UrlEncodedFormEntity uefEntity ;
			uefEntity = new UrlEncodedFormEntity(fromParams,"UTF-8") ;
			//设置
			httpPost.setEntity(uefEntity) ;
		}
		
		/*
		 * 执行
		 */
		String html = null ;
		response = httpClient.execute(httpPost);
		
		HttpEntity httpEntity;
		httpEntity = response.getEntity();
        if(httpEntity != null){
            html = readHtmlContentFromEntity(httpEntity);
        }
        
		/*
		 * 返回结果
		 */
		//获取头信息
		//Header[] headers=response.getHeaders("Location");
		return html ;
	}
	
	
	
	/**
     * 从response返回的实体中读取页面代码
     * @param httpEntity Http实体
     * @return 页面代码
     * @throws ParseException
     * @throws IOException
     */
    private static String readHtmlContentFromEntity(HttpEntity httpEntity) throws ParseException, IOException {
        String html = "";
        Header header = httpEntity.getContentEncoding();
        if(httpEntity.getContentLength() < 2147483647L){            //EntityUtils无法处理ContentLength超过2147483647L的Entity
            if(header != null && "gzip".equals(header.getValue())){
                html = EntityUtils.toString(new GzipDecompressingEntity(httpEntity));
            } else {
                html = EntityUtils.toString(httpEntity);
            }
        } else {
            InputStream in = httpEntity.getContent();
            if(header != null && "gzip".equals(header.getValue())){
                html = unZip(in, ContentType.getOrDefault(httpEntity).getCharset().toString());
            } else {
                html = readInStreamToString(in, ContentType.getOrDefault(httpEntity).getCharset().toString());
            }
            if(in != null){
                in.close();
            }
        }
        return html;
    }
    
    
    /**
     * 解压服务器返回的gzip流
     * @param in 抓取返回的InputStream流
     * @param charSet 页面内容编码
     * @return 页面内容的String格式
     * @throws IOException
     */
    private static String unZip(InputStream in, String charSet) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPInputStream gis = null;
        try {
            gis = new GZIPInputStream(in);
            byte[] _byte = new byte[1024];
            int len = 0;
            while ((len = gis.read(_byte)) != -1) {
                baos.write(_byte, 0, len);
            }
            String unzipString = new String(baos.toByteArray(), charSet);
            return unzipString;
        } finally {
            if (gis != null) {
                gis.close();
            }
            if(baos != null){
                baos.close();
            }
        }
    }
    /**
     * 读取InputStream流
     * @param in InputStream流
     * @return 从流中读取的String
     * @throws IOException
     */
    private static String readInStreamToString(InputStream in, String charSet) throws IOException {
        StringBuilder str = new StringBuilder();
        String line;
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, charSet));
        while((line = bufferedReader.readLine()) != null){
            str.append(line);
            str.append("\n");
        }
        if(bufferedReader != null) {
            bufferedReader.close();
        }
        return str.toString();
    }
}


HttpURLUtils.java
package net.itaem.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Scanner;

/**
 * 
 * UrlConnection抓包
 * @author sen
 * @version 1.0,2014年8月28日
 */
public class HttpURLUtils {

	/**
	 * 
	 * 1.GET请求
	 * @author sen
	 * @param urlStr url地址
	 * @return 返回信息
	 * @throws IOException
	 */
	public static String doGet(String urlStr) throws IOException{
		
		//获取连接对象
		URLConnection connection = getURLConnection(urlStr) ;
		
		//返回接收信息
		return receiveMsg(connection) ;
		
	}
	
	/**
	 * 
	 * 2.POST请求
	 * @author sen
	 * @param urlStr url地址
	 * @param params 请求参数
	 * @return 返回信息
	 * @throws IOException
	 */
	public static String doPost(String urlStr,Map<String,String> params) throws IOException{
		
		//获取连接对象
		URLConnection connection = getURLConnection(urlStr) ;
		//发送数据
		sendMsg(connection,params);
		
		//返回接收信息
		return receiveMsg(connection) ;
	}
	
	
	
	
	
	/**
	 * 
	 * 获取URLConnection连接对象
	 * @author sen
	 * @param urlStr url地址
	 * @return
	 * @throws IOException
	 */
	private static URLConnection getURLConnection(String urlStr) throws IOException{
		//实例化URL对象
		URL url = new URL(urlStr) ;
		//获取URLConnection对象
		URLConnection connection = url.openConnection() ;
		//初始化HttpURLConnection对象
		//HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		//设置请求头信息
		connection.setRequestProperty("Accept-Charset", "gbk");
		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset="+"gbk");
		//返回对象
		return connection ;
	}
	
	/**
	 * 
	 * 发送信息
	 * @author sen
	 * @param connection 连接对象
	 * @param params
	 * @throws IOException
	 */
	private static void sendMsg(URLConnection connection,Map<String,String> params) throws IOException{
		//设置为可向服务器发送信息
		connection.setDoOutput(true);
		/*
		 * POST请求发送数据
		 */
		//获取输出流,使客户端也可以想服务器进行写操作
		if(params!=null && params.size()>0){
			PrintWriter out = new PrintWriter(connection.getOutputStream()) ;
			boolean first = true ;
			for(Map.Entry<String, String> param : params.entrySet()){
				if(first)
					first = false ;
				else
					out.print('&');
				String name = param.getKey() ;
				String value = param.getValue() ;
				
				out.print(name);
				out.print('=');
				out.print(URLEncoder.encode(value,"UTF-8")); //发送键值对
			}
			
			out.close(); //关闭输出流
		}
	}
	
	/**
	 * 
	 * 接收信息
	 * @author sen
	 * @param connection 连接对象
	 * @return
	 * @throws IOException
	 */
	private static String receiveMsg(URLConnection connection) throws IOException{
		/*
		 * 接收返回信息
		 */
		Scanner in ;
		StringBuilder response = new StringBuilder() ;
		try{
			in = new Scanner(connection.getInputStream()) ;
		}catch(IOException e){
			if(!(connection instanceof HttpURLConnection))
				throw e ;
			InputStream error = ((HttpURLConnection)connection).getErrorStream() ;
			if(error == null)
				throw e ;
			in = new Scanner(error) ;
		}
		
		while(in.hasNextLine()){
			response.append(in.nextLine()) ;
			response.append("\n") ;
		}
		
		in.close();
		return response.toString() ;
	}
	
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值