乖乖,servlet不就是干这活的么。难道你想开http端口进行监听???

解决方案 »

  1.   

    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;
        }
    }
      

  2.   

    实例化Comm是传入目的url和包含参数的Hashtable,然后调用postToServer()返回包含返回信息的Hashtable。
      

  3.   

    楼上的这位老兄真能写,其实servlet的老本行就是搞这个的
    servlet中有很多api可供你获取http报文头.看看api吧!很多的.
      

  4.   

    getProtocol()方法获取传输协议及版本
    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的信息
    ==
      

  5.   

    servlet的信息只能用于自己的应用系统中,对于别的系统的请求信息却无能为力!
    chenweionline(键盘上的舞者)的答案可行!