有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

php用Java发送POST数据

我正在尝试用POST从Java提交表单。我想发送一些值并将它们存储在Web服务器上的sql db中。我在堆栈中找到了这个示例,但我没有正确理解它

URL是否应该引用接受POST请求的php文件?我还需要考虑其他的价值观。我使用的参数没有问题,它们应该与我的PHP文件上的POST检查相匹配

这就是成功地将POST数据从Java发送到我的Web服务器所需的全部吗

URL url = new URL("http://g.php");
        Map<String,Object> params = new LinkedHashMap<>();
        params.put("value", 5);
        params.put("id", 17);

        StringBuilder postData = new StringBuilder();
        for (Map.Entry<String,Object> param : params.entrySet()) {
            if (postData.length() != 0) postData.append('&');
            postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
            postData.append('=');
            postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
        }
        byte[] postDataBytes = postData.toString().getBytes("UTF-8");

        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
        conn.setDoOutput(true);
        conn.getOutputStream().write(postDataBytes);

        Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

        for (int c; (c = in.read()) >= 0;)
            System.out.print((char)c);

共 (2) 个答案

  1. # 1 楼答案

    下面的例子对我很有用

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("http://example.com/");
    // Request parameters and other properties.
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("user", "Bob"));
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        // writing error to Log
        e.printStackTrace();
    }
    
  2. # 2 楼答案

    相同,但有注释:

        URL url = new URL("http://g.php"); // URL to your application
        Map<String,Object> params = new LinkedHashMap<>();
        params.put("value", 5); // All parameters, also easy
        params.put("id", 17);
    
        StringBuilder postData = new StringBuilder();
        // POST as urlencoded is basically key-value pairs, as with GET
        // This creates key=value&key=value&... pairs
        for (Map.Entry<String,Object> param : params.entrySet()) {
            if (postData.length() != 0) postData.append('&');
            postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
            postData.append('=');
            postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
        }
    
        // Convert string to byte array, as it should be sent
        byte[] postDataBytes = postData.toString().getBytes("UTF-8");
    
        // Connect, easy
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        // Tell server that this is POST and in which format is the data
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
        conn.setDoOutput(true);
        conn.getOutputStream().write(postDataBytes);
    
        // This gets the output from your server
        Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
    
        for (int c; (c = in.read()) >= 0;)
            System.out.print((char)c);