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(); } }
详细解决方案
模拟ie发送http讯息
热度:117 发布时间:2012-11-12 12:31:56.0
相关解决方案
- struts HTTP Status 500解决办法
- httpclient老是提示java.lang.NoSuchMethodError: org.apache.http.protocol.BasicHttpCon
- 启动tomcat后,http://localhost:8080无法打开的有关问题
- 相干tomcat HTTP Status 500的疑问,麻烦大家进来看看
- Web Service 错误:服务器未能识别 HTTP 头 SOAPAction 的值: http://tempuri.org/
- HTTP Status 404 - /jspdemo/Register.jsp,该怎么解决
- 'http://reg.163.com/login.jsp?type=1&url=http://entry.mail.163.com/coremail/fcg/,该如何处理
- org.apache.http.client.HttpClient与org.apache.commons.httpclient.HttpClient的差别
- HTTP 404有关问题
- myeclipse<%@ taglib prefix="c" uri="http://java.sun.com/jst1/core_rt"%>解决思路
- ,http 200状态码
- tomcat新手有关问题 HTTP Status 404 - /manager/
- java http 分段上载
- soap调用webservice,报错:Root element of a SOAP message must be: 'http://schemas.xmls,该怎么处理
- HTTP Status 404如何解决
- org.apache.tomcat.util.http.Parameters processParameters,该怎么解决
- 新手,HTTP Status 404 有关问题
- java http 断点上传,该如何处理
- JSP疑问<%@ taglib prefix="ht" uri="http://www.ht.com.cn/jsp/taglibs"%>该怎么处理
- weservice调用Server returned HTTP response code: 500 for URL解决方法
- HTTP Status 404 - /webproject2/servlet/login解决办法
- 报错:HTTP Status 405 - HTTP method GET is not supported by this URL解决思路
- http://newbbs.csdn.net/ 现时和老版的bbs用的还不是同一个库
- http://newbbs.csdn.net/ 现下和老版的bbs用的还不是同一个库
- Java.io.IOException: error 3 during http over WSP 这个是什么东东
- http://sourceforge.net这个网站是什么啊解决方案
- http 传送数据 每个包有大小限制码?解决办法
- Server returned HTTP response code: 403 for URL如何解决
- http://www.java-home.com重新下线,诚邀您的加入
- httpClient 错误 The server failed to respond with a valid HTTP response