自己动手利用Socket 实现HTTP与HTTPS

大致分了几个步骤 一:当然是要知道HTTP协议,知道常用的HTTP请求头,比如Host, Accept, Content-Type
知道HTTP协议支持的方法,常用有GET/POST/PUT/DELETE等
如果不知道,也不用担心,我保证你读完这篇文章,你就知道一些啦,当然有个最好的参考
文档就是HTTP相关的RFC文档,认真读一下肯定解决你自己心中的HTTP那些疑惑
二: 知道发送HTTP GET与POST格式很重要, 固定的格式如下:
[REQUEST][HTTP VERSION]
[REQUEST HEADER: ][VALUE]
可以有多个请求头
最后
发送完HTTP请求头部以后, 针对不同请求如POST要发送内容部分,发送完成以后同样
结尾.
解释: 表示空格, 表示回车换行Java中表示为”\r\n”
REQUEST表示HTTP请求命令,可以为POST, GET, PUT, DELETE等之一
HTTP VERSION的常见可能值为HTTP/1.1或者HTTP/1.0
三: 如果1与2的知识你都具备了,下面就来介绍一下JAVA Socket的相关知识
如何创建一个JAVA客户端套接字Socket s = new Socket()如此即可,简单吧!
如何连接到远程的主机与端口, 当提供URL字符串时候,可以这么做
URL url = new URL(“http://blog.csdn.NET/jia20003”);
String host = url.getHost;
int port = url.getDefaultPort();
SocketAddress dest = new InetSocketAddress(this.host, this.port);
s.connect(dest);
即可连接到远程主机,下面就可以请求文章内容了, 也很容易的


  1. String path = "/jia20003/article/details/16908661";
  2. SocketAddress dest = new InetSocketAddress(this.host, this.port);
  3. socket.connect(dest);
  4. OutputStreamWriter streamWriter = new OutputStreamWriter(socket.getOutputStream());
  5. bufferedWriter = new BufferedWriter(streamWriter);
  6. bufferedWriter.write("GET " + path + " HTTP/1.1\r\n");
  7. bufferedWriter.write("Host: " + this.host + "\r\n");
  8. bufferedWriter.write("\r\n");
  9. bufferedWriter.flush();
最后也是最重要的一点,字符编码,尽量都用同一种字符编码来发 送请求数据,推荐使用utf-8测试程序, 我写两个简单的PHP文件放
在wamp server上,完成对全部代码的测试
四:关于HTTP与HTTPS
HTTP协议是位于第四层协议TCP之上完成的应用层协议, 端到端都是明文传送,别人一
旦网络抓包以后都可以看到你的提交与请求数据,这个好像不太安全. HTTP协议的默认
端口是80这个是RFC文档声明的,属于官方标准,没什么道理可以讲.
HTTPS是基于SSL加密传输的,这样别人截获你的数据包破解的概率要小一点,比HTTP安
全一点,其默认端口是443, 好像QQ邮箱与谷歌的WEB Mail邮箱都是基于HTTPS. 但是
HTTPS通信方式只是传输数据加密,都客户端来说是透明的,它还是一样要遵守HTTP协
议规范来发送POST与GET请求等.
完整的测试程序如下:
【自己动手利用Socket 实现HTTP与HTTPS】
[java] view plain copy
  1. package com.gloomyfish.http.client;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStreamWriter;
  8. import java.net.InetSocketAddress;
  9. import java.net.Socket;
  10. import java.net.SocketAddress;
  11. import java.net.URLEncoder;
  12. public class TestDemo {
  13. private int port;
  14. private String host;
  15. private Socket socket;
  16. private BufferedReader bufferedReader;
  17. private BufferedWriter bufferedWriter;
  18. public TestDemo(String host, int port) {
  19. socket = new Socket();
  20. this.host = host;
  21. this.port = port;
  22. }
  23. public void sendGet() throws IOException
  24. {
  25. String path = "/zhigang/getDemo.php";
  26. SocketAddress dest = new InetSocketAddress(this.host, this.port);
  27. socket.connect(dest);
  28. OutputStreamWriter streamWriter = new OutputStreamWriter(socket.getOutputStream());
  29. bufferedWriter = new BufferedWriter(streamWriter);
  30. bufferedWriter.write("GET " + path + " HTTP/1.1\r\n");
  31. bufferedWriter.write("Host: " + this.host + "\r\n");
  32. bufferedWriter.write("\r\n");
  33. bufferedWriter.flush();
  34. BufferedInputStream streamReader = new BufferedInputStream(socket.getInputStream());
  35. bufferedReader = new BufferedReader(new InputStreamReader(streamReader, "utf-8"));
  36. String line = null;
  37. while((line = bufferedReader.readLine())!= null)
  38. {
  39. System.out.println(line);
  40. }
  41. bufferedReader.close();
  42. bufferedWriter.close();
  43. socket.close();
  44. }
  45. public void sendPost() throws IOException
  46. {
  47. String path = "/zhigang/postDemo.php";
  48. String data = https://www.it610.com/article/URLEncoder.encode("name", "utf-8") + "=" + URLEncoder.encode("gloomyfish", "utf-8") + "&" +
  49. URLEncoder.encode("age", "utf-8") + "=" + URLEncoder.encode("32", "utf-8");
  50. // String data = "https://www.it610.com/article/name=zhigang_jia";
  51. SocketAddress dest = new InetSocketAddress(this.host, this.port);
  52. socket.connect(dest);
  53. OutputStreamWriter streamWriter = new OutputStreamWriter(socket.getOutputStream(), "utf-8");
  54. bufferedWriter = new BufferedWriter(streamWriter);
  55. bufferedWriter.write("POST " + path + " HTTP/1.1\r\n");
  56. bufferedWriter.write("Host: " + this.host + "\r\n");
  57. bufferedWriter.write("Content-Length: " + data.length() + "\r\n");
  58. bufferedWriter.write("Content-Type: application/x-www-form-urlencoded\r\n");
  59. bufferedWriter.write("\r\n");
  60. bufferedWriter.write(data);
  61. bufferedWriter.flush();
  62. bufferedWriter.write("\r\n");
  63. bufferedWriter.flush();
  64. BufferedInputStream streamReader = new BufferedInputStream(socket.getInputStream());
  65. bufferedReader = new BufferedReader(new InputStreamReader(streamReader, "utf-8"));
  66. String line = null;
  67. while((line = bufferedReader.readLine())!= null)
  68. {
  69. System.out.println(line);
  70. }
  71. bufferedReader.close();
  72. bufferedWriter.close();
  73. socket.close();
  74. }
  75. public static void main(String[] args)
  76. {
  77. TestDemo td = new TestDemo("127.0.0.1",8099);
  78. try {
  79. // td.sendGet(); //send HTTP GET Request
  80. td.sendPost(); // send HTTP POST Request
  81. } catch (IOException e) {
  82. // TODO Auto-generated catch block
  83. e.printStackTrace();
  84. }
  85. }
  86. }
想要成为支持HTTPS客户端, 只要在创建Socket的时候如下:
  1. socket = (SSLSocket)((SSLSocketFactory)SSLSocketFactory.getDefault()).createSocket(this.host, port);

    推荐阅读