【问题标题】:Java send POST data (POST data set but empty)Java 发送 POST 数据(POST 数据集但为空)
【发布时间】:2011-10-18 20:02:33
【问题描述】:

我正在尝试使用 java 发送 POST 数据。这是我尝试使用的方法:

public void doSubmit(String url, HashMap<String, String> data) throws Exception {
    URL siteUrl = new URL(url);
    HttpURLConnection conn = (HttpURLConnection) siteUrl.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    conn.setDoInput(true);

    DataOutputStream out = new DataOutputStream(conn.getOutputStream());

    Set keys = data.keySet();
    Iterator keyIter = keys.iterator();
    String content = "";
    for(int i=0; keyIter.hasNext(); i++) {
        Object key = keyIter.next();
        if(i!=0) {
            content += "&";
        }
        content += key + "=" + URLEncoder.encode(data.get(key), "UTF-8");
    }
    System.out.println(content);
    out.writeBytes(content);
    out.flush();
    out.close();
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line = "";
    while((line=in.readLine())!=null) {
        System.out.println(line);
    }
    in.close();
}

这是我测试它的方式:

 HashMap<String, String> data = new HashMap<String, String>();
 data.put("img", "someuser");

 doSubmit("http://www.blah.com/b.php", data);

在 .php 中进行一些回显测试后,设置了 POST 数据,但 POST 数组为空。为什么?

【问题讨论】:

    标签: java php http-post httpurlconnection


    【解决方案1】:

    尝试设置

    connection.setRequestProperty("Accept-Charset", 'UTF-8');
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
    

    【讨论】:

    • 那行不通。我添加了,POST 数据仍然是空的。
    【解决方案2】:

    我遇到了完全相同的问题,在这里找到了解决方案:http://digitallibraryworld.com/?p=189

    //import these on your header
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.ProtocolException;
    import java.net.URL;
    import java.net.URLEncoder;
    import java.util.Scanner;
    //do this wherever you are wanting to POST
    URL url;
    HttpURLConnection conn;
    
    try{
    //if you are using https, make sure to import java.net.HttpsURLConnection
    url=new URL("http://somesite/somefile.php");
    
    //you need to encode ONLY the values of the parameters
    String param="param1=" + URLEncoder.encode("value1","UTF-8")+
    "&param2="+URLEncoder.encode("value2","UTF-8")+
    "&param3="+URLEncoder.encode("value3","UTF-8");
    
    conn=(HttpURLConnection)loginUrl.openConnection();
    //set the output to true, indicating you are outputting(uploading) POST data
    conn.setDoOutput(true);
    //once you set the output to true, you don't really need to set the request method to post, but I'm doing it anyway
    conn.setRequestMethod("POST");
    
    //Android documentation suggested that you set the length of the data you are sending to the server, BUT
    // do NOT specify this length in the header by using conn.setRequestProperty("Content-Length", length);
    //use this instead.
    conn.setFixedLengthStreamingMode(param.getBytes().length);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    //send the POST out
    PrintWriter out = new PrintWriter(conn.getOutputStream());
    out.print(param);
    out.close();
    
    //build the string to store the response text from the server
    String response= "";
    
    //start listening to the stream
    Scanner inStream = new Scanner(conn.getInputStream());
    
    //process the stream and store it in StringBuilder
    while(inStream.hasNextLine())
    response+=(inStream.nextLine());
    
    }
    //catch some error
    catch(MalformedURLException ex){  
    Toast.makeText(MyActivity.this, ex.toString(), 1 ).show();
    
    }
    // and some more
    catch(IOException ex){
    
    Toast.makeText(MyActivity.this, ex.toString(), 1 ).show();
    }
    

    我所做的与示例所做的主要区别在于它生成一个字符串并对其进行 URL 编码 - 我在此生成 navevaluepairs 然后将它们转换为字符串。 它也正确发送字节的长度

    conn.setFixedLengthStreamingMode(param.getBytes().length);
    

    我不在的地方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      相关资源
      最近更新 更多