蒲公英联盟提供ghost win10,ghost win7,xp,装机纯净版,64位旗舰版,绿色软件,免费软件下载基地!

当前位置:快三内蒙古专家推荐 > 教程 > 服务器类 >

内蒙快3.31期三期计划: 在利用Java网络编程时

来源:蒲公英系统┆发布时间:2018-11-01 12:10┆点击:

快三内蒙古专家推荐 www.btfqn.cn 在利用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

尚未注册畅言帐号,请到后台注册
  • 苗山脱贫影像志——父母在 不远行 2019-03-20
  • 秒变迷弟!吴建豪晒与甄子丹合影:偶像给我签名 2019-03-14
  • 秦珂:联盟不是生态圈本身,是生态圈的过程 2019-03-14
  • “陪堂妈妈”上课3000多节 班主任:她去中考没问题 2018-08-22
  • 城区加大扬尘污染治理力度 所有施工现场必须安装视频监控 2018-08-22
  • 张继科状态低迷 刘国梁倍感压力 2018-07-26
  • 互联网金融协会提示:防范变相“现金贷”业务风险 2018-07-26
  • 6月乌鲁木齐这些片区要停电 2018-07-25
  • 补时绝杀!英格兰2-1突尼斯 凯恩梅开二度 2018-07-24
  • 张太雷烈士大学毕业证书回到故乡常州 2018-07-24
  • 对于马克思主义者来说,什么也不值得惋惜,历史教训才值得的总结 2018-07-23
  • 少了两个气缸,这台跑车依然能做你的Dream Car 2018-07-23
  • 上海频繁"出镜"好莱坞 大片里重要的"未来"城市 2018-07-22
  • 回复@海之宁:不给你分配生产资料你咋尽所能? 2018-07-21
  • 惊险!28吨浓亚硫酸泄露 消防官兵9小时排险 2018-07-21
  • 25| 517| 857| 197| 393| 468| 455| 226| 191| 861|