问一下啊,我想做一个网站群发信息的桌面jar软件,
信息内容要post方式传递,怎样才能传递过去啊?比如
<input type="text" name="one" value="aaaaaaa" />
将文本框one中的数据"aaaaaaa"通过POST方式传递至网页http://www.baidu.com要怎么能够传递过去啊?

解决方案 »

  1.   

    http://hc.apache.org/httpclient-3.x/
      

  2.   

    使用Socket
    try {
        // Construct data
        String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
        data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");    // Create a socket to the host
        String hostname = "hostname.com";
        int port = 80;
        InetAddress addr = InetAddress.getByName(hostname);
        Socket socket = new Socket(addr, port);    // Send header
        String path = "/servlet/SomeServlet";
        BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
        wr.write("POST "+path+" HTTP/1.0\r\n");
        wr.write("Content-Length: "+data.length()+"\r\n");
        wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
        wr.write("\r\n");    // Send data
        wr.write(data);
        wr.flush();    // Get response
        BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            // Process line...
        }
        wr.close();
        rd.close();
    } catch (Exception e) {
    }
    ==========================================================
    使用URL
    try {
        // Construct data
        String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
        data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");    // Send data
        URL url = new URL("http://hostname:80/cgi");
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();    // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            // Process line...
        }
        wr.close();
        rd.close();
    } catch (Exception e) {
    }