2017-08-31 41 views
6

代码的目标是将参数插入到表单中,然后提交表单,然后将表单输入到MySQL数据库中。问题是该方法不会发布数据。我不确定我做错了什么,我已经看过这么多的问题,但似乎没有任何工作。无法使用Java发送帖子表格

这里是表格。

<form action="http://localhost/Documents/dataadded.php" method="post"> 

<b>Add a New Data</b> 

<p>Email Address: 
<input type="text" name="email_address" size="30" value="" /> 
</p> 

<p>Email Pass: 
<input type="text" name="email_pass" size="30" value="" /> 
</p> 

<p> 
<input type="submit" name="submit" value="Send" /> 
</p> 

</form> 

这是Java代码。

public static void main(String[] args) { 


    String key1 = "email_address"; 
    String key2 = "email_pass"; 
    String key3 = "submit"; 

    String param1 = "[email protected]"; 
    String param2 = "password123"; 
    String param3 = "Send"; 
    try { 
     URL website = new URL("http://localhost/Documents/added.php"); 

     Map<String,String> arguments = new LinkedHashMap<>(); 
     arguments.put(key1, param1); 
     arguments.put(key2, param2); 
     arguments.put(key3, param3); 

     StringJoiner sj = new StringJoiner("&"); 
     for(Map.Entry<String,String> entry : arguments.entrySet()) 
      sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "=" 
       + URLEncoder.encode(entry.getValue(), "UTF-8")); 
     byte[] out = sj.toString().getBytes(StandardCharsets.UTF_8); 
     int length = out.length; 

     HttpURLConnection connection = (HttpURLConnection) website.openConnection(); 
     connection.setRequestMethod("POST"); 
     connection.setFixedLengthStreamingMode(length); 
     connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
     connection.setDoOutput(true); 
     connection.getOutputStream().write(out); 


     System.out.println(sj.toString()); 

     InputStream response = connection.getInputStream(); 

     @SuppressWarnings("resource") 
     Scanner scan = new Scanner(response); 
     String responsebody = scan.useDelimiter("\\A").next(); 
     System.out.println(responsebody); 



    } catch (IOException e) { 

     e.printStackTrace(); 
    } 

} 

如果有人可以对代码有什么问题进行说明,那么将不胜感激。

+1

PHP如何介入? –

+0

当表单被提交时,数据被传递给http://localhost/Documents/dataadded.php,后者具有处理数据并将其添加到数据库的PHP代码。 –

+1

@ C.Trant但是这实际上与你的问题有关吗?如何处理数据看起来毫不相关,您的问题是传递数据 – GrumpyCrouton

回答

0

在打开InputStream之前,必须刷新OutputStream。以下是我为执行POST请求而创建的方法。

private String doPost(String urlString, LinkedHashMap<String, String> params) throws Exception { 
    //...make the content 
    StringBuilder content = new StringBuilder(); 
    //...append params 
    boolean first = true; 
    for (Map.Entry<String, String> entry : params.entrySet()) { 
     if (!first) { 
      content.append("&"); 
     } else { 
      first = false; 
     } 
     content.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), charset)); 
    } 
    //... send POST request 
    URL url = new URL(urlString); 
    HttpURLConnection con; 
    con = (HttpURLConnection) url.openConnection(); 
    con.setDoInput(true); 
    con.setDoOutput(true); 
    con.setUseCaches(false); 
    try (OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream(), charset)) { 
     writer.write(content.toString()); 
     writer.flush(); 
    } 
    //...get response 
    try (Scanner s = new Scanner(con.getInputStream(), charset)) { 
     String resp = s.useDelimiter("\\A").hasNext() ? s.next() : ""; 
     return resp; 
    } 
} 
+0

这是Java 7的代码,请原谅旧的for循环。 –

+0

try-with-resources关闭输出流,并关闭输出流来刷新它,并且你肯定不应该在循环内部刷新。 – EJP

+0

你的回答指出你必须刷新。你没有。 – EJP