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();
}
}
}
}