`
hepx
  • 浏览: 149595 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

socket模拟http请求

    博客分类:
  • web
阅读更多


/**
 * socket模拟http 请求
 * @author xixi
 * 注意:请求的目标端要实现GET和POST的方法。IP和端口适自己的情况定
 * 
 * 用来测试,没有封装。
 */
public static void doGet(){
try {
//如果传中文参数请转码
String data = "pid=1002&pcode=K001001";
// Create a socket to the host
String hostname = "localhost";
int port = 8088;
InetAddress addr = InetAddress.getByName(hostname);
Socket socket = new Socket(addr, port);
  
// Send header
String path = "/kinyb/doMW2?"+data;
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "GBK"));
wr.write("GET "+path+" HTTP/1.0\r\n");
wr.write("\r\n");

wr.flush();
  
// Get response
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream(),"GBK"));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
wr.close();
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
 * socket模拟http post 请求
 * @author xixi
 */
public static void doPost(){
try {
  
String data = "pid=1002&pcode=K001001";
// Create a socket to the host
String hostname = "localhost";
int port = 8088;
InetAddress addr = InetAddress.getByName(hostname);
Socket socket = new Socket(addr, port);
  
// Send header
String path = "/kinyb/doMW2";
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "GBK"));
wr.write("POST "+path+" HTTP/1.0\r\n");
wr.write("Content-Length: "+data.length()+"\r\n");
        wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
wr.write("\r\n");
// Send data
wr.write(data);
wr.flush();
  
// Get response
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream(),"GBK"));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
wr.close();
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
 * @author xixi
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
//doGet();
doPost();


}
 

分享到:
评论

相关推荐

    C#基于socket模拟http请求的方法

    本文实例讲述了C#基于socket模拟http请求的方法。分享给大家供大家参考。具体实现方法如下: using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; ...

    JAVA SOCKET模拟HTTP请求,文件上传!

    采用JAVA的socket实现http协议,模拟浏览器访问服务端。

    socket http/https 模拟登录 请求页面等

    socket http/https 模拟登录 请求页面等

    PHP socket 模拟POST 请求实例代码

    我们用到最多的模拟POST请求几乎都是使用php curl来实现了,没考虑到PHP socket也可以实现,今天看到朋友写了一文章,下面我来给大家分享一下PHP socket模拟POST请求实例。 以前模拟post请求俺都用PHP curl扩展实现...

    socket模拟HTTP协议头

    套接字方式模拟HTTP请求,向WEB服务器发送协议头,并接收返回信息。

    四款网络通信测试工具-含socket/post/get http请求

    博客:【安卓学习之工具学习】网络通信测试工具-socket/post/get 的附件,博客连接为:http://blog.csdn.net/ljb568838953/article/details/52493675 附件里面含: fiddlersetup_v4.6.2.0.32002.exe NetAssist_v4.2....

    易语言HTTP连接请求源码

    易语言HTTP连接请求源码

    使用PHP Socket 编程模拟Http post和get请求

    主要介绍了使用PHP Socket 编程模拟Http post和get请求 ,需要的朋友可以参考下

    向web服务器请求HTTP页面的mfc单文档程序

    向web服务器请求HTTP页面的mfc单文档程序 使用Socket编程产生一个HTTP的请求报文,将接收到的报文内容显示在文档视图中。

    一个Http请求处理工具 爬虫

    本工具是上一个http请求处理工具的更新,用socket来模拟http, 一个Http请求处理工具 Socket C#消息队列的创建 读取远程消息队列 socket长连接 http长连接

    Socket实现Http上传文件的代码

    该代码实现了对于socket发送http请求上传文件的封装,且上传了一个模拟服务端的php代码,在 test目录,首先需要自己下载PHPStudy安装好,然后将下载下来的资源中test目录的文件复制到php目录下www目录中,启动...

    利用python的socket发送http(s)请求方法示例

    主要给大家介绍了关于利用python的socket发送http(s)请求的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用python具有一定的参考学习价值,需要的朋友们下面来一起看看吧

    PHP模拟http请求的方法详解

    本文实例讲述了PHP模拟http请求的方法。分享给大家供大家参考,具体如下: 方法一:利用php的socket编程来直接给接口发送数据来模拟post的操作。 建立两个文件post.php,getpost.php post.php内容如下: <?php $...

    IIS简单模拟(C#)

    产生一个新的代理socket和客户端进行通信(原来的侦听端口的socket释放,继续侦听其他客户端的请求)同时开启一个新的线程接收客户端消息 5.分析客户端的请求报文,对请求的文件返回响应报文 http/1.1中的长连接的...

    HttpClient:php使用socket模拟post、get操作,支持http、socket4、5代理

    GET请求 $ http = new HttpClient ();$ http -> set_header ( 'User-Agent' , 'Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1' ); $ http -> get ( 'http://www.example.com/' ); echo $ ...

    HTTP.zip_HTTP 调试_http socket_java http post

    模拟HTTP协议,Java 发送 http 请求 (get 与 post 方法请求 ) , 以下代码经本人亲自调试可用! 可以直接使用之。 注意:通过 BufferedReader 读取远程返回的数据时,必须设置读取编码,否则中文会乱码!

    sockettest

    监控发送http请求的详细信息,一般服务器打印信息不是很方便,用这个来模拟,还是不错的。

Global site tag (gtag.js) - Google Analytics