socket模拟http中post或者get提交数据,并获得响应数据

首先建立一个web工程 

准备连个jsp文件试试正常的流程 

testpost.jsp数据提交页面 

<%@ page language="java" pageEncoding="GBK"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head> 
  </head> 
  <body> 
  <form method="post" action="testpostresult.jsp"> 
    This is my JSP page. <br><br> 
    <input name="name" value="fruitking"/><br><br> 
    <input name="company" value="intohotel"/><br><br> 
    <input type="submit" value="submit"/><br> 
  </form> 
  </body> 
</html> 



testpostresult.jsp数据响应页面 

<%@ page language="java" pageEncoding="GBK"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head> 
  </head> 
  <body> 
    This is my JSP page. <br> 
    name:<%=request.getParameter("name")%><br> 
    company:<%=request.getParameter("company")%><br> 
  </body> 
</html> 




如果用socket如何建立post提交,get提交呢?如果又获得这个http响应呢? 

post提交模拟 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.InetAddress; 
import java.net.Socket; 
import java.net.URLEncoder; 

public class TestSocketPost { 

    public static void main(String[] args) { 
        BufferedWriter httpPostWriter = null; 
        BufferedReader httpResponse = null; 
        try { 
            // form域的数据.form域的数据必须以链接形式发送 
            StringBuffer formDataItems = new StringBuffer(); 
            formDataItems.append(URLEncoder.encode("name", "GBK")); 
            formDataItems.append("="); 
            formDataItems.append(URLEncoder.encode("fruitking", "GBK")); 
            formDataItems.append("&"); 
            formDataItems.append(URLEncoder.encode("company", "GBK")); 
            formDataItems.append("="); 
            formDataItems.append(URLEncoder.encode("intohotel", "GBK")); 
            String hostname = "localhost";// 主机,可以是域名,也可以是ip地址 
            int port = 8080;// 端口 
            InetAddress addr = InetAddress.getByName(hostname); 
            // 建立连接 
            Socket socket = new Socket(addr, port); 
            // 创建数据提交数据流 
            httpPostWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "GBK")); 
            // 相对主机的请求地址 
            String httpSubmitPath = "/icbcnet/testpostresult.jsp"; 
            // 发送数据头 
            httpPostWriter.write("POST " + httpSubmitPath + " HTTP/1.0\r\n"); 
            httpPostWriter.write("Host: socket方式的post提交测试\r\n"); 
            httpPostWriter.write("Content-Length: " + formDataItems.length() + "\r\n"); 
            httpPostWriter.write("Content-Type: application/x-www-form-urlencoded\r\n"); 
            httpPostWriter.write("\r\n"); // 以空行作为分割 
            // 发送数据 
            httpPostWriter.write(formDataItems.toString()); 
            httpPostWriter.flush(); 
            // 创建web服务器响应的数据流 
            httpResponse = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK")); 
            String lineStr = ""; 
            while ((lineStr = httpResponse.readLine()) != null) { 
                System.out.println(lineStr); 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            try { 
                if (httpPostWriter != null) { 
                    httpPostWriter.close(); 
                } 
                if (httpResponse != null) { 
                    httpResponse.close(); 
                } 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
        } 
    } 
} 

get模拟 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.InetAddress; 
import java.net.Socket; 
import java.net.URLEncoder; 

public class TestSocketGet { 

    public static void main(String[] args) { 
        BufferedWriter httpGetWriter = null; 
        BufferedReader httpResponse = null; 
        try { 
            String hostname = "localhost";// 主机,可以是域名,也可以是ip地址 
            int port = 8080;// 端口 
            InetAddress addr = InetAddress.getByName(hostname); 
            // 建立连接 
            Socket socket = new Socket(addr, port); 
            // 创建数据提交数据流 
            httpGetWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "GBK")); 
            // 相对主机的请求地址 
            StringBuffer httpSubmitPath = new StringBuffer("/icbcnet/testpostresult.jsp?"); 
            httpSubmitPath.append(URLEncoder.encode("name", "GBK")); 
            httpSubmitPath.append("="); 
            httpSubmitPath.append(URLEncoder.encode("fruitking", "GBK")); 
            httpSubmitPath.append("&"); 
            httpSubmitPath.append(URLEncoder.encode("company", "GBK")); 
            httpSubmitPath.append("="); 
            httpSubmitPath.append(URLEncoder.encode("pubone", "GBK")); 
            httpGetWriter.write("GET " + httpSubmitPath.toString() + " HTTP/1.1\r\n"); 
            httpGetWriter.write("Host: socket方式的get提交测试\r\n"); 
            httpGetWriter.write("\r\n"); 
            httpGetWriter.flush(); 
            // 创建web服务器响应的数据流 
            httpResponse = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK")); 
            // 读取每一行的数据.注意大部分端口操作都需要交互数据。 
            String lineStr = ""; 
            while ((lineStr = httpResponse.readLine()) != null) { 
                System.out.println(lineStr); 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            try { 
                if (httpGetWriter != null) { 
                    httpGetWriter.close(); 
                } 
                if (httpResponse != null) { 
                    httpResponse.close(); 
                } 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
        } 
    } 
} 

你可能感兴趣的:(socket)