模拟ie发送http消息


package cn.com.surekam;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpAccessJDBCClient {

private static String JDBC_QUERY = "http://localhost:8081/jdbc/?method=query&";
/**
* 发送http请求
* @throws IOException
*/
public static void sendHttp() throws IOException {
URL url = new URL(
"http://localhost:8080/ImitateIE/ie.do?password=33");// 设置要访问的链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 获取HttpURLConnection的对象
conn.setDoOutput(true); // 默认值为false,不能传递参数
conn.setRequestMethod("POST"); // 设置请求方式
conn.setRequestProperty("referer", "http://www.sina.com/index.html");
OutputStream out = conn.getOutputStream();
out.write("name=aaaa".getBytes());
// 向服务器发送一个值为"aaaa"的name参数,如果conn的DoOutput属性值为false,此处将抛出异常
conn.getResponseCode(); // 获取响应状态码
System.out.println("sendHttp:conn.getResponseCode():"+conn.getResponseCode());
}

/**
* 发送http请求,接受http请求
* @throws IOException
*/
public static void sendAndReadHttp() throws IOException {
URL url = new URL(
"http://localhost:8080/ImitateIE/ie.do?");// 设置要访问的链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 获取HttpURLConnection的对象
conn.setDoOutput(true); // 默认值为false,不能传递参数
conn.setRequestMethod("POST"); // 设置请求方式
conn.setRequestProperty("referer", "http://www.sina.com/index.html");
OutputStream out = conn.getOutputStream();
out.write("name=name&password=password".getBytes());
// 向服务器发送一个值为"aaaa"的name参数,如果conn的DoOutput属性值为false,此处将抛出异常
conn.getResponseCode(); // 获取响应状态码
System.out.println("sendHttp:conn.getResponseCode():"+conn.getResponseCode());
InputStream in = conn.getInputStream(); // 获取一个和服务器返回的内容相关联的流
try {
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) > 0) {
System.out.println(new String(buffer, 0, len)); // 输出到控制台
}
} finally {
if (in != null)
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

/**
* 读取http请求的信息
* @throws IOException
*/
public static void readHttp() throws IOException {
URL url = new URL(
"http://localhost:8080/ImitateIE/ie.do"); // 设置请求的链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
System.out.println(conn.getResponseCode()); // 查看响应状态码
System.out.println(conn.getHeaderField("Content-Length")); // 响应文本内容的长度
InputStream in = conn.getInputStream(); // 获取一个和服务器返回的内容相关联的流
try {
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) > 0) {
System.out.println(new String(buffer, 0, len)); // 输出到控制台
}
} finally {
if (in != null)
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

/**
* 发送http请求
* @throws IOException
*/
public static void sendHttpToJDBC() throws IOException {
URL url = new URL("http://localhost:8080/jdbc/?method=query&appId=998");// 设置要访问的链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 获取HttpURLConnection的对象
conn.setDoOutput(true); // 默认值为false,不能传递参数
conn.setRequestMethod("POST"); // 设置请求方式
conn.setRequestProperty("referer", "http://www.sina.com/index.html");
OutputStream out = conn.getOutputStream();
out.write("appId=998".getBytes());
// 向服务器发送一个值为"aaaa"的name参数,如果conn的DoOutput属性值为false,此处将抛出异常
conn.getResponseCode(); // 获取响应状态码
System.out.println("conn.getResponseCode():"+conn.getResponseCode());
System.out.println("conn.getResponseCode():"+conn.getContent());

InputStream in = conn.getInputStream(); // 获取一个和服务器返回的内容相关联的流
try {
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) > 0) {
System.out.println(new String(buffer, 0, len)); // 输出到控制台
}
} finally {
if (in != null)
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

/**
* 读取http请求的信息
* @throws IOException
*/
public static void readHttpFromJDBC() throws IOException {
StringBuffer bufUrl = new StringBuffer(JDBC_QUERY);
bufUrl.append("appId=998&appName=用户验证系统");
// URL url = new URL(bufUrl.toString()); // 设置请求的链接

URL url = new URL(new String(bufUrl.toString().getBytes("UTF-8"),"UTF-8")); // 设置请求的链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// conn.setDoOutput(true); // 默认值为false,不能传递参数
// OutputStream out = conn.getOutputStream();
// out.write("name=aaaa".getBytes());
System.out.println(conn.getResponseCode()); // 查看响应状态码
System.out.println(conn.getHeaderField("Content-Length")); // 响应文本内容的长度
System.out.println(conn.getContentEncoding()); // 响应文本内容的编码
System.out.println(conn.getConnectTimeout()+" content:"+conn.getContent().toString()); // 响应时间
System.out.println("url:"+url.toString());
System.out.println("url:"+url.toString());

InputStream in = conn.getInputStream(); // 获取一个和服务器返回的内容相关联的流
try {
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) > 0) {
System.out.println(new String(buffer, 0, len)); // 输出到控制台
}
} finally {
if (in != null)
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
// sendHttp();
// readHttp();
sendAndReadHttp();
// readHttpFromJDBC();
// sendHttpToJDBC();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值