【问题标题】:Sending Post info to php file (Java)将帖子信息发送到 php 文件 (Java)
【发布时间】:2013-02-25 18:24:05
【问题描述】:

如何将 Post 值发送到 PHP 文件?

以便 PHP 文件可以将此值发送到例如数据库或其他东西。

这样我就可以将一个字符串从我的 java 文件发送到使用它的 php 文件。

【问题讨论】:

    标签: java php post send


    【解决方案1】:

    正如我前段时间回答的那样,我为此做了一个课程,希望对你有所帮助。

    但是,您应该向我们展示您的尝试,而不是仅仅询问。

    import java.io.*;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLEncoder;
    import java.util.Vector;
    
    public class SiteFX {
    
       private String postParameters = "";
       private String webPage;
       private Vector<String>names;
       private Vector<String>values;
    
       public SiteFX(){
          values = new Vector<String>();
          names = new Vector<String>();
       }
    
       /**
        * Adds a post variable (page.php?name=value)
        *
        * @param name the variable name
        * @param value the variable value, can be set to null, the url will simply become &name instead of &name=value
        * null
        */
       public void addPostValue(String name, String value) {
          if (value == null) {
             try {
                postParameters += "&" + URLEncoder.encode(name, "UTF-8");
                names.add(name);
                values.add("");
             } catch (Exception ex) {
                ex.printStackTrace();
             }
          } else {
             postParameters += "&" + URLEncoder.encode(name, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8");
             names.add(name);
             values.add(value);
          }
       }
    
       /**
        * Send post data without waiting for site output
        *
        * @return true if sending data terminated succesfully
        */
       public boolean sendPost() {
          try {
             if (webPage == null || webPage.equals("")) {
                throw new Exception("Empty url");
             }
             URL url = new URL(webPage);
             URLConnection conn = url.openConnection();
             conn.setDoOutput(true);
             OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
             wr.write(postParameters);
             wr.flush();
          } catch (Exception e) {
             e.printStackTrace();
             return false;
          }
          postParameters = "";
          return true;
       }
    
       /**
        * Sends data, then waits for site output
        *
        * @return null if no data is received, or a String containing the data
        */
       public String sendPostWithReturnValue() {
    
          String returnValue = "";
          try {
             if (webPage == null || webPage.equals("")) {
                throw new Exception("Empty url");
             }
             URL url = new URL(webPage);
             URLConnection conn =
                     url.openConnection();
             conn.setDoOutput(true);
             OutputStreamWriter wr =
                     new OutputStreamWriter(conn.getOutputStream());
             wr.write(postParameters);
             wr.flush();
             BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
             String line;
             while ((line = rd.readLine()) != null) {
                returnValue += line + "\n";
             }
             wr.close();
             rd.close();
          } catch (Exception e) {
             e.printStackTrace();
             return null;
          }
          postParameters = "";
          values = null;
          names=null;
          values = new Vector<String>();
          names = new Vector<String>();
          return returnValue;
       }
    
       /**
        * Sets the page to point at for sending post variables
        *
        * @param webPageToPointAt the page that will receive your post data
        */
       public void setWebPageToPointAt(String webPageToPointAt) {
          webPage = webPageToPointAt;
       }
    
       /**
        * @returns A Nx2 matrix containing all parameters name and values
        */
       public String[][] getParameters() {
          String[][] str = new String[names.size()][2];
          for (int i = 0; i < str.length; i++) {
             str[i][0] = names.get(i);
             str[i][1] = values.get(i);
          }
          return str;
       }
    }
    

    【讨论】:

    • 但这似乎是一个 get 方法??
    • 不,这是一个 post 请求类。你什么也没找到,因为你甚至没有搜索 Google - Java post
    猜你喜欢
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 2012-03-21
    • 2011-02-19
    • 1970-01-01
    相关资源
    最近更新 更多