-2

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

这样 PHP 文件就可以将此值发送到数据库或其他东西。

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

4

1 回答 1

0

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

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

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;
   }
}
于 2013-03-10T15:37:29.230 回答