Http发帖请求php脚本

时间:2015-03-27 01:28:52

标签: java php apache http post

我有一个java程序,它对我网站上的php脚本发出http post请求。这是代码。我也在使用新的Apache HTTP Components API。这是下载链接 - > http://hc.apache.org/downloads.cgi

public static void main(String args[]) throws ClientProtocolException, IOException
{
HttpPost httppost = new HttpPost("http://ftstoo.com/public_html/TheFinalTouchSecurity/Database/test.php");

List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
parameters.add(new BasicNameValuePair("name", "Cannon"));


httppost.setEntity(new UrlEncodedFormEntity(parameters));

HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(httppost);

HttpEntity resEntity = httpResponse.getEntity();

// Get the HTTP Status Code
int statusCode = httpResponse.getStatusLine().getStatusCode();

// Get the contents of the response
InputStream in = resEntity.getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder out = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
        out.append(line);
    }
    System.out.println(out.toString());   //Prints the string content read  from input stream
    reader.close();
}

}

我的test.php文件位于http://ftstoo.com/public_html/TheFinalTouchSecurity/Database/test.php

这是我的PHP代码。

 <?php  
$name = $_POST['name'];
echo "Success, . $name . !";
 ?> 

我运行java程序时得到的输出是我网站上重定向页面的html,表示该页面不存在。我希望输出返回一个字符串,上面写着“Success,Cannon!”我想可能有几件事我做错了,但是如果有人可以请我帮忙,我会非常感激!

0 个答案:

没有答案