简体   繁体   English

用 Java 发送 HTTP POST 请求并用 PHP 接受 POST 请求

[英]Sending an HTTP POST request in Java and accepting the POST request in PHP

What I need to do is send a username and password to a php script via a HTTP POST request so that I can query a database for the correct information.我需要做的是通过 HTTP POST 请求将用户名和密码发送到 php 脚本,以便我可以查询数据库以获取正确的信息。 Currently I am stuck on both sending the POST request as well as receiving it.目前我被困在发送 POST 请求和接收它上。

To send a username and password I am using the following:要发送用户名和密码,我使用以下内容:

public class post {
    public static void main(String[] args) throws ClientProtocolException, IOException {
        HttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost("http://www.example.com/practice.php");

        // Request parameters and other properties.
        List<NameValuePair> params = new ArrayList<NameValuePair>(2);
        params.add(new BasicNameValuePair("username", "user"));
        params.add(new BasicNameValuePair("password", "hunter2"));
        httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

        //Execute and get the response.
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            InputStream instream = entity.getContent();
            try {
                // do something useful
            } finally {
                instream.close();
            }
        }

    }
}

The php script I am using to collect the information is the following, it's simple but it's just for testing at the moment.我用来收集信息的php脚本如下,它很简单,但目前仅用于测试。

<?php
$username = $_POST['username'];
$password = $_POST['password'];
echo "username = $username<br>";
echo "password = $password<br>";
 ?>

I was wondering if someone could help me out by moving me in the correct direction of accepting a HTTP POST request in php from Java, or if I am even sending the post request correctly, any help is greatly appreciated.我想知道是否有人可以通过将我移动到从 Java 接受 php 中的 HTTP POST 请求的正确方向来帮助我,或者如果我什至正确发送了 post 请求,非常感谢任何帮助。

Now, there are a few things, I would suggest you to keep in mind, while doing this.现在,我建议您在执行此操作时牢记以下几点。

  1. Try Making use of JSON: Json stands for JavaScript Object Notation .尝试使用 JSON: Json 代表JavaScript Object Notation Itis a lightweight, text-based, language-independent data exchange format that is easy for humans and machines to read and write.它是一种轻量级、基于文本、独立于语言的数据交换格式,易于人和机器读写。

     public static void main(String[] args){ JSONObject obj = new JSONObject(); obj.put("username", username); obj.put("password", password); System.out.print(obj); // And then, send this via POST Method. } }

    For Php part,对于 PHP 部分,

     ... $data = file_get_contents("php://input"); $json = json_decode($data); $username = $json['username']; $password = $json['password']; ...

Here is a good reference for that Json这是该Json 的一个很好的参考

  1. Make Use of Sessions When you work with an application, you open it, do some changes, and then you close it.使用会话当您使用应用程序时,您打开它,进行一些更改,然后关闭它。 This is much like a Session.这很像一个会话。 The computer knows who you are.计算机知道你是谁。 It knows when you start the application and when you end.它知道您何时启动应用程序以及何时结束。 But on the internet there is one problem: the web server does not know who you are or what you do, because the HTTP address doesn't maintain state.但是在 Internet 上存在一个问题:Web 服务器不知道您是谁或您在做什么,因为 HTTP 地址不维护状态。 Session variables solve this problem by storing user information to be used across multiple pages (eg username, favorite color, etc).会话变量通过存储跨多个页面使用的用户信息(例如用户名、最喜欢的颜色等)来解决这个问题。 By default, session variables last until the user closes the browser.默认情况下,会话变量持续到用户关闭浏览器。

     <?php $_SESSION["user"] = "green"; echo "Session variables are set."; // Now store this in your database in a separate table and set its expiry date and time. ?>

    Here is a reference to that as well sessions .这是对session的引用。

  2. Use SSL : Secure Socket Layer (SSL) technology is security that is implemented at the transport layer.SSL allows web browsers and web servers to communicate over a secure connection.使用 SSL安全套接字层 (SSL)技术是在传输层实现的安全性。SSL 允许 Web 浏览器和 Web 服务器通过安全连接进行通信。 In this secure connection, the data that is being sent is encrypted before being sent and then is decrypted upon receipt and before processing.在这种安全连接中,正在发送的数据在发送前被加密,然后在收到和处理前解密。 Both the browser and the server encrypt all traffic before sending any data.浏览器和服务器在发送任何数据之前都会加密所有流量。 SSL addresses the following important security considerations. SSL 解决了以下重要的安全注意事项。

a.一种。 Authentication : During your initial attempt to communicate with a web server over a secure connection, that server will present your web browser with a set of credentials in the form of a server certificate.身份验证:在您最初尝试通过安全连接与 Web 服务器通信时,该服务器将以服务器证书的形式向您的 Web 浏览器提供一组凭据。 The purpose of the certificate is to verify that the site is who and what it claims to be.证书的目的是验证该站点是谁以及它声称的内容。 In some cases, the server may request a certificate that the client is who and what it claims to be (which is known as client authentication).在某些情况下,服务器可能会请求一个证书,表明客户端是谁以及它声称是什么(这称为客户端身份验证)。

b.Confidentiality : When data is being passed between the client and the server on a network, third parties can view and intercept this data.机密性:当数据在网络上的客户端和服务器之间传递时,第三方可以查看和拦截这些数据。 SSL responses are encrypted so that the data cannot be deciphered by the third party and the data remains confidential. SSL 响应经过加密,因此第三方无法破译数据并且数据保持机密。

c. C。 Integrity : When data is being passed between the client and the server on a network, third parties can view and intercept this data.完整性:当数据在网络上的客户端和服务器之间传递时,第三方可以查看和拦截这些数据。 SSL helps guarantee that the data will not be modified in transit by that third party. SSL 有助于保证该第三方在传输过程中不会修改数据。

And, Here are a few references for that as well.而且,这里也有一些参考资料。 SSL Establishment Documentation , SSL with Java SSL 建立文档, SSL with Java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM