蒲公英聯盟提供ghost win10,ghost win7,xp,裝機純凈版,64位旗艦版,綠色軟件,免費軟件下載基地!

當前位置:星际争霸重制版98元有人买吗 > 教程 > 服務器類 >

星际争霸2官方小说txt: 在利用Java網絡編程時

來源:蒲公英系統┆發布時間:2018-11-01 12:10┆點擊:

星际争霸重制版98元有人买吗 www.peger.icu 在利用Java網絡編程時,利用Java獲取HTTP Request 和 Response頭字段;

可以利用Java語言根據需要添加自定義的HTTP頭字段,而不必拘泥于標準HTTP定義的頭字段。

代碼如下:

public class TestURL { public static void main(String[] args) { String destURLStr= ""; URL destURL = null; URLConnection urlCon = null; HttpURLConnection httpUrlCon= null; String readResFile = "C:/Users/zhoujw/Desktop/readResFile.html"; BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(readResFile)); destURL = new URL(destURLStr); urlCon = destURL.openConnection(); httpUrlCon = (HttpURLConnection)urlCon; //set request property httpUrlCon.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch"); //可根據需要添加自定義請求頭 httpUrlCon.setRequestProperty("Test Header1", "test1"); httpUrlCon.setRequestProperty("Test Header2", "test2"); httpUrlCon.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(httpUrlCon.getInputStream(), "gbk")); String webpage = null; while((( webpage = br.readLine()) != null)) { // System.out.println(webpage); bw.write(webpage); bw.flush(); } //debug System.out.println("Self Define Headers:"); System.out.println(" Test Header1: " + httpUrlCon.getRequestProperty("Test Header1")); System.out.println(" Test Header2: " + httpUrlCon.getRequestProperty("Test Header2")); System.out.println(); //echo request property echoRequestHeaders(httpUrlCon); //echo response property echoResponseHeaders(httpUrlCon); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void echoRequestHeaders(HttpURLConnection httpUrlCon){ System.out.println("Request Headers:"); System.out.println(" " + httpUrlCon.getRequestMethod() + " / " + " HTTP/1.1"); System.out.println(" Host: " + httpUrlCon.getRequestProperty("Host")); System.out.println(" Connection: " + httpUrlCon.getRequestProperty("Connection")); System.out.println(" Accept: " + httpUrlCon.getRequestProperty("Accept")); System.out.println(" User-Agent: " + httpUrlCon.getRequestProperty("User-Agent")); System.out.println(" Accept-Encoding: " + httpUrlCon.getRequestProperty("Accept-Encoding")); System.out.println(" Accept-Language: " + httpUrlCon.getRequestProperty("Accept-Language")); System.out.println(" Cookie: " + httpUrlCon.getRequestProperty("Cookie")); System.out.println(" Connection: " + httpUrlCon.getHeaderField("Connection"));//利用另一種讀取HTTP頭字段 System.out.println(); } public static void echoResponseHeaders(HttpURLConnection httpUrlCon) throws IOException{ System.out.println("Response Headers:"); System.out.println(" " + "HTTP/1.1 " + httpUrlCon.getResponseCode() + " " + httpUrlCon.getResponseMessage()); System.out.println(" status: " + httpUrlCon.getResponseCode() + " " + httpUrlCon.getResponseMessage()); System.out.println(" content-encoding: " + httpUrlCon.getContentEncoding()); System.out.println(" content-length : " + httpUrlCon.getContentLength()); System.out.println(" content-type: " + httpUrlCon.getContentType()); System.out.println(" Date: " + httpUrlCon.getDate()); System.out.println(" ConnectTimeout: " + httpUrlCon.getConnectTimeout()); System.out.println(" expires: " + httpUrlCon.getExpiration()); System.out.println(" content-type: " + httpUrlCon.getHeaderField("content-type"));//利用另一種讀取HTTP頭字段 System.out.println(); } }

運行結果:

Self Define Headers: Test Header1: test1 Test Header2: test2 Request Headers: GET / HTTP/1.1 Host: Connection: keep-alive Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 User-Agent: Java/1.6.0_20 Accept-Encoding: gzip,deflate,sdch Accept-Language: null Cookie: null Connection: Keep-Alive Response Headers: HTTP/1.1 200 OK status: 200 OK content-encoding: gzip content-length : -1 content-type: text/html; charset=utf-8 Date: 1427817028000 ConnectTimeout: 0 expires: 1427817001000 content-type: text/html; charset=utf-8

尚未注冊暢言帳號,請到后臺注冊