`

socket-client-http-post

阅读更多

目标:通过socket 模拟HTTP协议连接 tomcat,做一次POST请求

 

http://dl2.iteye.com/upload/attachment/0124/5410/145b9806-1e1d-3805-a11a-fc622c95e484.jpg 

 

请求数据:

POST /index.jsp HTTP/1.1
Host:192.168.237.230
Content-Type:application/x-www-form-urlencoded
Connection: close
Content-Length:42

username=xinchun.wang&password=123456.....

 

响应数据:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=613C816BEC2DB87DB7A043574B68AA57; Path=/
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 293
Date: Tue, 25 Apr 2017 02:58:49 GMT
Connection: close


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 xinchun.wang
  123456.....
</body>
</html>

 

    代码参考:

public class ClientPost {

	public static void main(String[] args) {
		sendRequest(8078);
	}

	public static void sendRequest(int port) {
		try {
			Socket socket = new Socket("192.168.237.230", port);

			OutputStream out = socket.getOutputStream();

			// 由Socket对象得到输入流,并构造相应的BufferedReader对象
			BufferedReader in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));

			String body = "username=xinchun.wang&password=123456.....";
			
			StringBuilder sb = new StringBuilder();
			sb.append("POST /index.jsp HTTP/1.1\r\n");
			sb.append("Host:192.168.237.230\r\n");
			sb.append("Content-Type:application/x-www-form-urlencoded\r\n");
			sb.append("Connection: close\r\n");
			sb.append("Content-Length:"+body.length()+"\r\n");
			sb.append("\r\n");

			sb.append(body);
			out.write(sb.toString().getBytes());

			out.flush();
			String line = in.readLine();
			while (line != null && line.toString() != "") {
				System.out.println(line);
				line = in.readLine();
			}

			out.close(); // 关闭Socket输出流
			in.close(); // 关闭Socket输入流
			socket.close(); // 关闭Socket

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

0
2
分享到:
评论

相关推荐

    http-client-socket-java:科大COMP4621计算机通讯网络I(2014Spring)项目

    生成 HTTP GET 和 POST 请求 读取HTTP响应 将 HTTP 响应负载保存到本地文件 ======== 测试网站 数据格式:名称=str str 可以是任何字符串(例如 str=Wei) 通过 HTTP GET 和 POST 将数据传送到此 URL

    smart-http 是一款可编程的 Http 应用微内核,基于smart-socket实现的轻量级http服务器

    smart-http 是一款可编程的 Http 应用微内核,基于smart-socket实现的轻量级http服务器,方便用户根据自身需求进行 Server 或 Client 的应用开发。支持GET、POST的 HTTP 请求。提供了 URL 路由组件,可以快速搭建...

    wget-1.11.4-1

    --post-data=STRING use the POST method; send STRING as the data. --post-file=FILE use the POST method; send contents of FILE. --content-disposition honor the Content-Disposition header when ...

    SocketTool2.rar

    SocketTool是一款小巧实用且功能强大的TCP/UDP网络通讯调试工具,绿色免费且无需安装,通过它可以帮助你...可以创建创建Socket服务器,创建UDP组播地址及端口,创建UDP Client客户端,可指定对方IP和端口,本地端口。

    Python-FastCGI-Client:用于Python的FastCGI客户端(直接与fastcgi-server(例如PHP-FPM等)进行通信)注意

    client = FastCGIClient('127.0.0.1', 9000, 3000, 0) params = dict() documentRoot = "/Users/baidu/php_workspace" uri = "/echo.php" content = "name=john&address=beijing" params = {'GATEWAY_INTERFACE': '...

    java微信支付开发所用jar包

    org.apache.http.client.methods.HttpPost; org.apache.http.config.RegistryBuilder; org.apache.http.conn.socket.ConnectionSocketFactory; org.apache.http.conn.socket.PlainConnectionSocketFactory; org...

    ICS delphixe10源码版

    .\Samples\delphi\SocketDemos Delphi Win32/Win64 Socket sample applications (all Delphi versions) .\Samples\delphi\sslinternet Delphi Win32/Win64 SSL-enabled sample applications (all Delphi versions) ....

    feedme:使用websockets的发布订阅系统

    post ( '/auth' , { json : { user : 'foo' , password : 'bar' }} , function ( e , r , b ) { socket . emit ( 'join' , b . token )} )发布事件var socket = require ( 'socket.io-client' ) ( '...

    php.ini-development

    data to the client. If your application's output exceeds this setting, PHP ; will send that data in chunks of roughly the size you specify. ; Turning on this setting and managing its maximum buffer ...

    java微信支付开发jar包

    org.apache.http.client.methods.HttpPost; org.apache.http.config.RegistryBuilder; org.apache.http.conn.socket.ConnectionSocketFactory; org.apache.http.conn.socket.PlainConnectionSocketFactory; org...

    Bochs - The cross platform IA-32 (x86) emulator

    (according to document from http://software.intel.com/en-us/avx/) - Rewritten from scratch SMC detection algorithm - Implemented fine-grained SMC detection (on 128 byte granularity) - Bugfixes for...

    vue发送websocket请求和http post请求的实例代码

    先给大家介绍下vue发送websocket请求和http post请求 直接上代码: pdf.vue [removed] import SockJS from 'sockjs-client'; import Stomp from 'stompjs'; import qs from "qs" export default { name: "pdf...

    计算机网络第六版答案

    Please do not post any solutions on a publicly-available Web site. We’ll be happy to provide a copy (up-to-date) of this solution manual ourselves to anyone who asks. Acknowledgments: Over the ...

    Clever Internet Suite (SRC) v9.1.0.0

    The Clever Internet Suite components allow you to compose and parse messages in MIME format with multiple file attachments, build and send Web Form POST requests and many other features. The SMTP, ...

    HttpClient以及获取页面内容应用

    HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceJ.action"); // 创建参数队列 List&lt;NameValuePair&gt; formparams = new ArrayList(); formparams.add(new BasicNameValuePair...

    HTTP请求库java-requests.zip

    Requests, 是一个模仿python requests 模块来设计的Http lib,拥有简单而灵活的API,在容易使用的同时,又能够满足各种高级定制的使用,可是说是当前最好用的Java Http Client Lib。 简单的请求示例:String url =...

    java 远程监控系统

    Socket client = new Socket(host,Integer.parseInt(post)); DataInputStream dis = new DataInputStream(client.getInputStream()); JFrame jframe = new JFrame("本地监控系统 - Charles"); ...

    php帮助文档,php。chm,php必备的中文手册

    PostScript document creation CXX. Pspell Functions CXXI. qtdom Functions CXXII. Radius CXXIII. Rar Functions CXXIV. GNU Readline CXXV. GNU Recode Functions CXXVI. RPM Header Reading 函数 CXXVII. ...

    php手册.chm,php手册

    CURL, Client URL Library Functions XIX. Cybercash Payment Functions XX. Cyrus IMAP administration Functions XXI. Database (dbm-style) Abstraction Layer Functions XXII. Date/Time 日期/时间函数 XXIII....

    NetClient.zip_C#编程_C#_

    Socket接口测试工具,支持GET POST POST UTB-8三种形式的接口测试。

Global site tag (gtag.js) - Google Analytics