超值问题! 用JAVA如何获取通过HTTP协议传递的内容(包含HTTP头内容,就是整个消息的内容) 乖乖,servlet不就是干这活的么。难道你想开http端口进行监听??? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.util.Hashtable;import java.net.URL;import java.net.NoRouteToHostException;import java.io.IOException;import java.net.ConnectException;import java.net.MalformedURLException;import java.net.HttpURLConnection;import java.net.URLEncoder;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Enumeration;import java.io.FileNotFoundException;import java.io.BufferedReader;import java.net.URLConnection;import java.io.PrintWriter;import java.net.UnknownHostException;public class Comm { private URL url; private Hashtable request; public Comm() { } public Comm(URL url, Hashtable request) { this.url = url; this.request = request; } public Hashtable postToServer() { Hashtable response = new Hashtable(); try { response = doPost(); } catch (ConnectException ex) { response.put("Exception", "ConnectException"); System.out.println("#### Communication Error£ºConnectException ####"); } catch (NoRouteToHostException ex) { response.put("Exception", "NoRouteToHostException"); System.out.println( "#### Communication Error£ºNoRouteToHostException ####"); } catch (UnknownHostException ex) { response.put("Exception", "UnknownHostException"); System.out.println( "#### Communication Error£ºUnknownHostException ####"); } catch (MalformedURLException ex) { response.put("Exception", "MalformedURLException"); System.out.println( "#### Communication Error£ºMalformedURLException ####"); } catch (IOException ex) { response.put("Exception", "IOException"); System.out.println("#### Communication Error£ºIOException ####"); } catch (Exception ex) { response.put("Exception", "Exception"); System.out.println("#### Communication Error£ºException ####"); } finally { return response; } } private Hashtable doPost() throws Exception { URLConnection connection = this.url.openConnection(); connection.setDoOutput(true); PrintWriter out = new PrintWriter(connection.getOutputStream()); Enumeration enumKey = this.request.keys(); while (enumKey.hasMoreElements()) { String name = (String) enumKey.nextElement(); String value = (String) this.request.get(name); char ch; if (enumKey.hasMoreElements()) { ch = '&'; } else { ch = ' '; } out.print(name + "=" + URLEncoder.encode(value) + ch); } out.close(); BufferedReader in; try { in = new BufferedReader(new InputStreamReader(connection. getInputStream())); } catch (FileNotFoundException exception) { InputStream err = ((HttpURLConnection) connection).getErrorStream(); if (err == null) { throw exception; } in = new BufferedReader(new InputStreamReader(err)); } Hashtable response = new Hashtable(); String line = ""; while ((line = in.readLine()) != null) { int i = line.indexOf("="); if (i != -1) { response.put(line.substring(0, i), line.substring(i + 1)); } } in.close(); return response; }} 实例化Comm是传入目的url和包含参数的Hashtable,然后调用postToServer()返回包含返回信息的Hashtable。 楼上的这位老兄真能写,其实servlet的老本行就是搞这个的servlet中有很多api可供你获取http报文头.看看api吧!很多的. getProtocol()方法获取传输协议及版本getRemoteAddr()方法返回因特网协议(IP)地址getRemoteHost()方法返回主机名getRemoteUser()方法返回用户名getScheme()方法返回模式名getServerName()方法返回服务器名getServerPort()方法返回服务器端口号req.getAuthtype()方法返回认证模式req.getMethod()方法返回HTTP方法req.getPathInfo()方法返回路径信息req.getPathTranslated()方法返回真实传输路径req.getQueryString()方法返回请求索引串req.getRequestURI()方法返回请求URIreq.getServletPath()方法返回Servlet路径req.getAttributeNames()方法返回请求属性集req.getAttribute(String name)方法返回属性name的信息== servlet的信息只能用于自己的应用系统中,对于别的系统的请求信息却无能为力!chenweionline(键盘上的舞者)的答案可行! 输入输出流如何直接对接? iBATIS SQL 语句 关于打包成jar文件的问题 jsp+servlet问题 哪位★娇娃★帮我解释一下“com.sun.*”属于什么包? 在线求救!2003要激活才能登陆! 如何在进程Process pcommand= Runtime.getRuntime().exec(cmd);中创建线程? 我想让结果集selectQuery的指针移到首位置,我该用何方法? 关于classpath的疑惑 求助:用JHepWork做非线性拟合报错:“/ is a folder” 我来看看java编程有多少地方要 Java如何能截获某个端口的数据!!
import java.net.URL;
import java.net.NoRouteToHostException;
import java.io.IOException;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.net.URLConnection;
import java.io.PrintWriter;
import java.net.UnknownHostException;public class Comm {
private URL url;
private Hashtable request; public Comm() {
} public Comm(URL url, Hashtable request) {
this.url = url;
this.request = request;
} public Hashtable postToServer() {
Hashtable response = new Hashtable();
try {
response = doPost();
} catch (ConnectException ex) {
response.put("Exception", "ConnectException");
System.out.println("#### Communication Error£ºConnectException ####");
} catch (NoRouteToHostException ex) {
response.put("Exception", "NoRouteToHostException");
System.out.println(
"#### Communication Error£ºNoRouteToHostException ####");
} catch (UnknownHostException ex) {
response.put("Exception", "UnknownHostException");
System.out.println(
"#### Communication Error£ºUnknownHostException ####");
} catch (MalformedURLException ex) {
response.put("Exception", "MalformedURLException");
System.out.println(
"#### Communication Error£ºMalformedURLException ####");
} catch (IOException ex) {
response.put("Exception", "IOException");
System.out.println("#### Communication Error£ºIOException ####");
} catch (Exception ex) {
response.put("Exception", "Exception");
System.out.println("#### Communication Error£ºException ####");
} finally {
return response;
}
} private Hashtable doPost() throws Exception {
URLConnection connection = this.url.openConnection(); connection.setDoOutput(true); PrintWriter out = new PrintWriter(connection.getOutputStream()); Enumeration enumKey = this.request.keys(); while (enumKey.hasMoreElements()) {
String name = (String) enumKey.nextElement();
String value = (String) this.request.get(name);
char ch;
if (enumKey.hasMoreElements()) {
ch = '&';
} else {
ch = ' ';
} out.print(name + "=" + URLEncoder.encode(value) + ch);
} out.close(); BufferedReader in;
try {
in = new BufferedReader(new InputStreamReader(connection.
getInputStream()));
} catch (FileNotFoundException exception) {
InputStream err = ((HttpURLConnection) connection).getErrorStream();
if (err == null) {
throw exception;
}
in = new BufferedReader(new InputStreamReader(err));
}
Hashtable response = new Hashtable();
String line = ""; while ((line = in.readLine()) != null) {
int i = line.indexOf("=");
if (i != -1) {
response.put(line.substring(0, i), line.substring(i + 1));
}
} in.close(); return response;
}
}
servlet中有很多api可供你获取http报文头.看看api吧!很多的.
getRemoteAddr()方法返回因特网协议(IP)地址
getRemoteHost()方法返回主机名
getRemoteUser()方法返回用户名
getScheme()方法返回模式名
getServerName()方法返回服务器名
getServerPort()方法返回服务器端口号
req.getAuthtype()方法返回认证模式
req.getMethod()方法返回HTTP方法
req.getPathInfo()方法返回路径信息
req.getPathTranslated()方法返回真实传输路径
req.getQueryString()方法返回请求索引串
req.getRequestURI()方法返回请求URI
req.getServletPath()方法返回Servlet路径
req.getAttributeNames()方法返回请求属性集
req.getAttribute(String name)方法返回属性name的信息
==
chenweionline(键盘上的舞者)的答案可行!