我试图使用套接字从Java客户端发布一些数据。它会与运行php代码的localhost进行交流,它只是将发送给它的后续参数吐出。Http Post不发布数据
这里是Java客户端:
public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost", 8888);
String reqStr = "testString";
String urlParameters = URLEncoder.encode("myparam="+reqStr, "UTF-8");
System.out.println("Params: " + urlParameters);
try {
Writer out = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
out.write("POST /post3.php HTTP/1.1\r\n");
out.write("Host: localhost:8888\r\n");
out.write("Content-Length: " + Integer.toString(urlParameters.getBytes().length) + "\r\n");
out.write("Content-Type: text/html\r\n\n");
out.write(urlParameters);
out.write("\r\n");
out.flush();
InputStream inputstream = socket.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
String string = null;
while ((string = bufferedreader.readLine()) != null) {
System.out.println("Received " + string);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
socket.close();
}
}
这是post3.php的样子:
<?php
$post = $_REQUEST;
echo print_r($post, true);
?>
我希望看到一个数组(myparams => “的TestString”)作为响应。但它没有通过发布参数到服务器。 这里输出:
Received HTTP/1.1 200 OK
Received Date: Thu, 25 Aug 2011 20:25:56 GMT
Received Server: Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.8r DAV/2 PHP/5.3.6
Received X-Powered-By: PHP/5.3.6
Received Content-Length: 10
Received Content-Type: text/html
Received
Received Array
Received (
Received)
只是一个FYI,这个设置适用于GET请求。
任何想法这里发生了什么?
只是想知道...有你不使用任何理由[了HTTPClient](http://hc.apache.org/httpcomponents-client-ga/ index.html的)? – chesles
或[Resty](http://beders.github.com/Resty/Resty/Overview.html)。 –
@ chesles-我正在通过SSL发送数据。 HttpClient-4的文档对设置定制的SSLSocketFactory并不是很有帮助。所以,在我用头部计算出用HttpClient4定制SSLSocketFactory之前,我正在试用普通套接字上的东西。 – bianca