一、发送Get请求
import java.net.*;
import java.io.*;
public class URLSender {
/** * @param args */
public static void main(String[] args) throws IOException {
try {
Socket socket = new Socket("www.nwu.edu.cn", 80);
boolean autoflush = true;
PrintWriter out = new PrintWriter(socket.getOutputStream(), autoflush);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//send an HTTP request to the web server
out.println("GET / HTTP/1.1");
out.println("Host: nwu.edu.cn");
out.println("Connection: Close");
out.println();
//read the response
boolean loop = true;
StringBuffer sb = new StringBuffer(8096);
while (loop) {
if (in.ready()) {
int i = 0;
while (i != -1) {
i = in.read();
sb.append((char) i);
}
loop = false;
}
//Thread.currentThread().sleep(50);
}
//display the response to the out console
System.out.println(sb.toString());
socket.close();
} catch (UnknownHostException e) {
System.err.println("Don't know about host: Victest.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for " + "the connection to: Victest.");
System.exit(1);
}
}
}
2、用Socket发送一个POST请求
try {
// Construct data
String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
// Create a socket to the host
String hostname = "hostname.com";
int port = 80;
InetAddress addr = InetAddress.getByName(hostname);
Socket socket = new Socket(addr, port);
// Send header
String path = "/servlet/SomeServlet";
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
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()));
String line;
while ((line = rd.readLine()) != null) {
// Process line...
}
wr.close();
rd.close();
} catch (Exception e) {
}
分享到:
相关推荐
在linux环境下,使用socket发送和接收http请求的例子
C语言socket发送http请求的例子源代码 一个使用C语言socket发送http请求的例子 这是一个linux下使用C的标准socket,进行http访问的例子。通过socket发送get请求,从中国天气网获得指定城市的天气。 该例子在ubuntu ...
通过socket发送get请求,从中国天气网获得指定城市的天气。 该例子在ubuntu 14.04和AM3358的嵌入式linux3.14下实际测试可用。 如果需要在嵌入式linux下使用,需要修改Makefile文件的CC和LINK项,指向相应的交叉...
C#中用SOCKET发送HTTP请求小例 自行组织http报文,报显示服务器反馈回来的报头与报体信息。 ------------- HTTP/1.1 200 OK Date: Mon, 08 Dec 2008 14:20:51 GMT Server: BWS/1.0
主要介绍了c#使用Socket发送HTTP/HTTPS请求的实现代码,需要的朋友可以参考下
基于java socket发送http请求。这种原生的发送请求的方式能适合一些特定场景,比如: A向B发送请求,A不考虑B是否处理成功,即A不关注B的响应结果,那么A就不用阻塞等B的回应。 传统的Httpclient请求方式都是阻塞...
NULL 博文链接:https://genius.iteye.com/blog/451378
本文实例讲述了PHP使用socket发送HTTP请求的方法。分享给大家供大家参考,具体如下: socket方式: $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); //socket_set_option($socket, SOL_SOCKET, SO_...
主要介绍了Python中使用socket发送HTTP请求数据接收不完整问题解决方法,本文使用一个循环解决了数据不完整问题,需要的朋友可以参考下
用Socket实现了发送HTTP请求,源码已经打包了 这些是CHttp里面公开的函数 void SetUrl(char*); //设置网页URL char* GetCookie(); //获取网页cookie void SetCookie(std::string strCookie);//添加cookie std::...
VC Socket GET_POST发送HTTP请求示例#资源达人分享计划#
挺适合新手看的,这是我刚接触c++时写的,新手也很容易看懂
VC Socket GET_POST发送HTTP请求示例
今天给大家带来的是如何利用socket发送GET,POST请求。我借用燕十八老师封装好的一个Http类给进行说明。 在日常编程中相信很多人和我一样大部分时间是利用浏览器向服务器提出GET,POST请求,那么可否利用其它方式...
开发环境:VS2013 C语言Socket实现HTTP协议,json格式数据组包 HTTP POST请求向服务器发送json数据
基于c socket 发送http请求