其实我是大概会写的,但我实在找不出我写的代码哪里错了= =求解
String content = "name=" + name + "&password=" + password;
byte[] datas = content.getBytes();
URL url = new URL(str_url);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", datas.length + "");
conn.setDoOutput(true);
// conn.setDoInput(true); oStream = conn.getOutputStream();
oStream.write(datas);
if (conn.getResponseCode() != 404) {
is = conn.getInputStream();
byte[] data = StreamTool.getStreamByte(is);
return new String(data);
}
return null;

解决方案 »

  1.   

    String content = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
        data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");使用实际的编码替换UTF-8
      

  2.   

    还是不行我觉得用get并且参数里面包含中文才需要编码一下
    那个代码实际上返回来的是302....我那个是一个jsp页面,输入后会跳转到一个servlet上。
    我用get可以实现,但post搞来搞去,还是不行我滴天啊
      

  3.   

    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://....");
        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) {
    }
      

  4.   

    吗的我知道哪里错了,我用get的时候返回来的是填写表单的html代码,当时就没注意,以为成功了我应该把地址改为servlet映射的路径才对,搞了一晚,吗的想死的心都有了= =