当前位置: 代码迷 >> Web前端 >> 模拟ie发送http讯息
  详细解决方案

模拟ie发送http讯息

热度:117   发布时间:2012-11-12 12:31:56.0
模拟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();
	}
}

  相关解决方案