1.httpURLConnection協議使用
1)創建一個URL對象
URL url = new URL(http://www.baidu.com);
2)利用HttpURLConnection對象從網絡中獲取網頁數據
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
3)設置連接超時
conn.setConnectTimeout(6*1000);
4)對響應碼進行判斷
if (conn.getResponseCode() != 200) //從Internet獲取網頁,發送請求,將網頁以流的形式讀回來
throw new RuntimeException("請求url失敗");
5)處理輸入輸出流
InputStream is = conn.getInputStream();
6)String result = readData(is, "GBK"); //文件流輸入出文件用outStream.write
7)conn.disconnect();
public class NetUtils { public static String post(String url, String content) { HttpURLConnection conn = null; try { // 創建一個URL對象 URL mURL = new URL(url); // 調用URL的openConnection()方法,獲取HttpURLConnection對象 conn = (HttpURLConnection) mURL.openConnection(); conn.setRequestMethod("POST");// 設置請求方法為post conn.setReadTimeout(5000);// 設置讀取超時為5秒 conn.setConnectTimeout(10000);// 設置連接網絡超時為10秒 conn.setDoOutput(true);// 設置此方法,允許向服務器輸出內容 // post請求的參數 String data = content; // 獲得一個輸出流,向服務器寫數據,默認情況下,系統不允許向服務器輸出內容 OutputStream out = conn.getOutputStream();// 獲得一個輸出流,向服務器寫數據 out.write(data.getBytes()); out.flush(); out.close(); int responseCode = conn.getResponseCode();// 調用此方法就不必再使用conn.connect()方法 if (responseCode == 200) { InputStream is = conn.getInputStream(); String response = getStringFromInputStream(is); return response; } else { throw new NetworkErrorException("response status is "+responseCode); } } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) { conn.disconnect();// 關閉連接 } } return null; } public static String get(String url) { HttpURLConnection conn = null; try { // 利用string url構建URL對象 URL mURL = new URL(url); conn = (HttpURLConnection) mURL.openConnection(); conn.setRequestMethod("GET"); conn.setReadTimeout(5000); conn.setConnectTimeout(10000); int responseCode = conn.getResponseCode(); if (responseCode == 200) { InputStream is = conn.getInputStream(); String response = getStringFromInputStream(is); return response; } else { throw new NetworkErrorException("response status is "+responseCode); } } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) { conn.disconnect(); } } return null; } private static String getStringFromInputStream(InputStream is) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); // 模板代碼 必須熟練 byte[] buffer = new byte[1024]; int len = -1; while ((len = is.read(buffer)) != -1) { os.write(buffer, 0, len); } is.close(); String state = os.toString();// 把流中的數據轉換成字符串,采用的編碼是utf-8(模擬器默認編碼) os.close(); return state; } }
2.httpClient的使用
1. 創建HttpClient對象。
2. 創建請求方法的實例,並指定請求URL。如果需要發送GET請求,創建HttpGet對象;如果需要發送POST請求,創建HttpPost對象。
3. 如果需要發送請求參數,可調用HttpGet、HttpPost共同的setParams(HetpParams params)方法來添加請求參數;對於HttpPost對象而言,也可調用setEntity(HttpEntity entity)方法來設置請求參數。
4. 調用HttpClient對象的execute(HttpUriRequest request)發送請求,該方法返回一個HttpResponse。
5. 調用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可獲取服務器的響應頭;調用HttpResponse的getEntity()方法可獲取HttpEntity對象,該對象包裝了服務器的響應內容。程序可通過該對象獲取服務器的響應內容。
6. 釋放連接。無論執行方法是否成功,都必須釋放連接
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.util.ArrayList; import java.util.List; import javax.net.ssl.SSLContext; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; 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.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.SSLContexts; import org.apache.http.conn.ssl.TrustSelfSignedStrategy; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; 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; import org.junit.Test; public class HttpClientTest { @Test public void jUnitTest() { get(); } /** * HttpClient連接SSL */ public void ssl() { CloseableHttpClient httpclient = null; try { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); FileInputStream instream = new FileInputStream(new File("d:\\tomcat.keystore")); try { // 加載keyStore d:\\tomcat.keystore trustStore.load(instream, "123456".toCharArray()); } catch (CertificateException e) { e.printStackTrace(); } finally { try { instream.close(); } catch (Exception ignore) { } } // 相信自己的CA和所有自簽名的證書 SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build(); // 只允許使用TLSv1協議 SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); // 創建http請求(get方式) HttpGet httpget = new HttpGet("https://localhost:8443/myDemo/Ajax/serivceJ.action"); System.out.println("executing request" + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); System.out.println(EntityUtils.toString(entity)); EntityUtils.consume(entity); } } finally { response.close(); } } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } finally { if (httpclient != null) { try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * post方式提交表單(模擬用戶登錄請求) */ public void postForm() { // 創建默認的httpClient實例. CloseableHttpClient httpclient = HttpClients.createDefault(); // 創建httppost HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceJ.action"); // 創建參數隊列 List<namevaluepair> formparams = new ArrayList<namevaluepair>(); formparams.add(new BasicNameValuePair("username", "admin")); formparams.add(new BasicNameValuePair("password", "123456")); UrlEncodedFormEntity uefEntity; try { uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); httppost.setEntity(uefEntity); System.out.println("executing request " + httppost.getURI()); CloseableHttpResponse response = httpclient.execute(httppost); try { HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println("--------------------------------------"); System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8")); System.out.println("--------------------------------------"); } } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 關閉連接,釋放資源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 發送 post請求訪問本地應用並根據傳遞參數不同返回不同結果 */ public void post() { // 創建默認的httpClient實例. CloseableHttpClient httpclient = HttpClients.createDefault(); // 創建httppost HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceJ.action"); // 創建參數隊列 List<namevaluepair> formparams = new ArrayList<namevaluepair>(); formparams.add(new BasicNameValuePair("type", "house")); UrlEncodedFormEntity uefEntity; try { uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); httppost.setEntity(uefEntity); System.out.println("executing request " + httppost.getURI()); CloseableHttpResponse response = httpclient.execute(httppost); try { HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println("--------------------------------------"); System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8")); System.out.println("--------------------------------------"); } } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 關閉連接,釋放資源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 發送 get請求 */ public void get() { CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 創建httpget. HttpGet httpget = new HttpGet("http://www.baidu.com/"); System.out.println("executing request " + httpget.getURI()); // 執行get請求. CloseableHttpResponse response = httpclient.execute(httpget); try { // 獲取響應實體 HttpEntity entity = response.getEntity(); System.out.println("--------------------------------------"); // 打印響應狀態 System.out.println(response.getStatusLine()); if (entity != null) { // 打印響應內容長度 System.out.println("Response content length: " + entity.getContentLength()); // 打印響應內容 System.out.println("Response content: " + EntityUtils.toString(entity)); } System.out.println("------------------------------------"); } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 關閉連接,釋放資源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 上傳文件 */ public void upload() { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceFile.action"); FileBody bin = new FileBody(new File("F:\\image\\sendpix0.jpg")); StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN); HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment).build(); httppost.setEntity(reqEntity); System.out.println("executing request " + httppost.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httppost); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { System.out.println("Response content length: " + resEntity.getContentLength()); } EntityUtils.consume(resEntity); } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } }
3.基於socket的網絡鏈接操作
(1)服務端
- 用指定的端口實例化一個SeverSocket對象。服務器就可以用這個端口監聽從客戶端發來的連接請求。
- 調用ServerSocket的accept()方法,以在等待連接期間造成阻塞,監聽連接從端口上發來的連接請求。
- 利用accept方法返回的客戶端的Socket對象,進行讀寫IO的操作
- 關閉打開的流和Socket對象
/** * 基於TCP協議的Socket通信,實現用戶登錄,服務端 */ //1、創建一個服務器端Socket,即ServerSocket,指定綁定的端口,並監聽此端口 ServerSocket serverSocket =newServerSocket(10086);//1024-65535的某個端口 //2、調用accept()方法開始監聽,等待客戶端的連接 Socket socket = serverSocket.accept(); //3、獲取輸入流,並讀取客戶端信息 InputStream is = socket.getInputStream(); InputStreamReader isr =newInputStreamReader(is); BufferedReader br =newBufferedReader(isr); String info =null; while((info=br.readLine())!=null){ System.out.println("Hello,我是服務器,客戶端說:"+info); } socket.shutdownInput();//關閉輸入流 //4、獲取輸出流,響應客戶端的請求 OutputStream os = socket.getOutputStream(); PrintWriter pw = new PrintWriter(os); pw.write("Hello World!"); pw.flush(); //5、關閉資源 pw.close(); os.close(); br.close(); isr.close(); is.close(); socket.close(); serverSocket.close();
(2)客戶端
- 用服務器的IP地址和端口號實例化Socket對象。
- 調用connect方法,連接到服務器上。
- 獲得Socket上的流,把流封裝進BufferedReader/PrintWriter的實例,以進行讀寫
- 利用Socket提供的getInputStream和getOutputStream方法,通過IO流對象,向服務器發送數據流
- 關閉打開的流和Socket。
//客戶端 //1、創建客戶端Socket,指定服務器地址和端口 Socket socket =newSocket("127.0.0.1",10086); //2、獲取輸出流,向服務器端發送信息 OutputStream os = socket.getOutputStream();//字節輸出流 PrintWriter pw =newPrintWriter(os);//將輸出流包裝成打印流 pw.write("用戶名:admin;密碼:admin"); pw.flush(); socket.shutdownOutput(); //3、獲取輸入流,並讀取服務器端的響應信息 InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String info = null; while((info=br.readLine())!null){ System.out.println("Hello,我是客戶端,服務器說:"+info); } //4、關閉資源 br.close(); is.close(); pw.close(); os.close(); socket.close();
(3)多線程
- 服務器端創建ServerSocket,循環調用accept()等待客戶端連接
- 客戶端創建一個socket並請求和服務器端連接
- 服務器端接受客戶端請求,創建socket與該客戶建立專線連接
- 建立連接的兩個socket在一個單獨的線程上對話
- 服務器端繼續等待新的連接
//服務器線程處理 和本線程相關的socket Socket socket =null; public serverThread(Socket socket){ this.socket = socket; } ServerSocket serverSocket =newServerSocket(10086); Socket socket =null; int count =0;//記錄客戶端的數量 while(true){ socket = serverScoket.accept(); ServerThread serverThread =newServerThread(socket); serverThread.start(); count++; System.out.println("客戶端連接的數量:"+count); }
