简体   繁体   English

PHP从Java接收POST请求

[英]PHP receiving a POST request from Java

EDIT: this is what is shown in my website logs xx.xx.xxx.xx - - [27/Jan/2012:17:42:24 -0500] "POST /dir/addData2.php HTTP/1.1" 200 - www.mywebsites.com "-" "Java/1.7.0" "-" 编辑:这是我的网站日志xx.xx.xxx.xx中显示的内容--[27 / Jan / 2012:17:42:24 -0500]“ POST /dir/addData2.php HTTP / 1.1” 200-www .mywebsites.com“-”“ Java / 1.7.0”“-”

I am hosting my website at 1&1, and I want to have page blank.php that should take a POST request and upload it to my database. 我将网站托管在1&1,并且我希望有一个blank.php页面,该页面应该接受POST请求并将其上传到我的数据库中。 I think I am sending my POST properly, and that somehow I am not handling it properly on my website. 我认为我正在正确发送POST,并且以某种方式我无法在我的网站上正确处理它。 Because nothing is being inserted to my database. 因为什么都没有插入到我的数据库中。 The response has content length 0, but even if i send a header with the length of the string it wont change. 响应的内容长度为0,但是即使我发送的标头的字符串长度也不会改变。 Another option is that the host wont allow me to do remote post requests (still waiting on reply). 另一个选择是,主持人不会允许我执行远程发布请求(仍在等待回复)。

I send the post request from a Java application like this: 我从这样的Java应用程序发送发布请求:

URL url = new URL("www.mywebsite.com/blank.php");    
HttpURLConnection request = (HttpURLConnection)url.openConnection();
request.setRequestProperty("Content-type","application/x-www-form-urlencoded");
request.setRequestMethod("POST");
OutputStreamWriter post = new OutputStreamWriter(request.getOutputStream());
String data = URLEncoder.encode("account", "UTF-8") + "=" + URLEncoder.encode(message[0], "UTF-8");
data += "&" + URLEncoder.encode("message", "UTF-8") + "=" + URLEncoder.encode(message[2], "UTF-8");
data += "&" + URLEncoder.encode("type", "UTF-8") + "=" + URLEncoder.encode(message[0], "UTF-8");
post.write(data);
post.flush();
/*
/ String example
/account=103&message=Feller+1391.88+0&type=103
*/

The response from the server is: 服务器的响应为:

null=[HTTP/1.1 200 OK]
Date=[Fri, 27 Jan 2012 21:59:10 GMT]
Content-Length=[0]
Keep-Alive=[timeout=2, max=200]
Connection=[Keep-Alive]
Content-Type=[text/html]
Server=[Apache]
X-Powered-By=[PHP/5.2.17]

My webpage has this basic PHP code (right now, will improve/check for stuff later) 我的网页上有这个基本的PHP代码(现在,以后会改进/检查内容)

$link = mysql_connect($hostname, $username, $password);
$db_selected = mysql_select_db($database, $link);
$query = "INSERT INTO newData(account, message, type) VALUES('$_POST[account]', '$_POST[message]', '$_POST[type]')";
mysql_query($query) || die();

I want to point out that I do not have a private server, and I will probably use http://hc.apache.org/httpcomponents-core-ga/examples.html later. 我想指出的是,我没有专用服务器,以后可能会使用http://hc.apache.org/httpcomponents-core-ga/examples.html Right now I just want to send a string from my Java application, receive it with PHP and insert to MySQL database. 现在,我只想从Java应用程序发送一个字符串,用PHP接收它,然后插入到MySQL数据库中。

Yes, I'm certain your ISP allows both "post" and "get" HTTP server requests. 是的,我确定您的ISP允许“发布”和“获取” HTTP服务器请求。

Superficially, both your Java and PHP look "OK". 从表面上看,您的Java和PHP都看起来“确定”。

STRONG SUGGESTION: 强烈建议:

1) Check your Apache logs on the server. 1)检查服务器上的Apache日志。

Make sure the request arrived (it probably did). 确保请求已到达(可能已经到达)。

Check for errors (there could well be none for your request - but you SHOULD see SOMETHING in the Apache error log. 检查错误(您的请求很可能没有任何错误-但是您应该在Apache错误日志中看到某些内容。

2) Verify PHP is working. 2)验证PHP是否正常工作。

Writing a "hello world" page with "phpinfo ();" 用“ phpinfo();”写一个“ hello world”页面 is a good way to do this. 是执行此操作的好方法。

3) Verify that MySQL is working. 3)验证MySQL是否正常工作。

Writing a "hello world" PHP page to verify you can 1) connect, 2) make a query, and 3) echo the query back in your web browser is ideal. 编写一个“ hello world” PHP页面以验证您可以进行以下操作:1)连接,2)进行查询,以及3)在Web浏览器中回显查询。

4) Verify that you can successfully read the "$_POST[account]" value you THINK you're getting from the client. 4)验证您可以成功读取从客户端获得的“ $ _POST [帐户]”值。

5) At that point, you should be able to: 5)到那时,您应该能够:

a) have the client communicate with your server a)让客户端与您的服务器通信

b) read the post request and write it to your database b)阅读发布请求并将其写入您的数据库

'Hope that helps .. and please post back with any specific questions/problems you might have. 希望对您有所帮助。请回传您可能遇到的任何具体问题。

You haven't told the client code that this is a POST request. 您尚未告诉客户端代码这是POST请求。 It will probably do a GET by default. 默认情况下,它可能会执行GET。

url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");

It looks like you're not setting the request method on your HttpUrlConnection, and the default is GET. 似乎您没有在HttpUrlConnection上设置request方法,默认值为GET。 You'd need something like 您需要类似

request.setRequestMethod("POST");

If that doesn't work, I'd consider rather using the Apache HTTP client right away from the beginning, it's easiert to use than Java's standard HTTP client API. 如果那不起作用,我会考虑从一开始就使用Apache HTTP客户端,它比Java的标准HTTP客户端API更容易使用。

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

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