我做POST请求,服务器接收并记录GET请求


I do POST request, server receives and logs GET request

在Android应用程序中,Java会发出POST请求,由php处理

这在本地工作时很好,apache会记录一个POST:

192.168.1.123 - - [29/Mar/2012:15:46:56 +0200] "POST /usuarioLogin.php HTTP/1.1" 200 292 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"

远程工作时,apache会记录一个GET请求,显然找不到发送的参数。Apache记录以下内容:

78.XXX.256.XXX - - [29/Mar/2012:16:20:05 +0200] "GET /usuarioLogin.php HTTP/1.1" 200 267 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"

执行POST的java代码:

private void HttpPost(String php, ArrayList<NameValuePair> nameValuePairs) {
    try {
        HttpClient httpclient = new DefaultHttpClient();
        String host = com.android.taggies.LoginUser.getContext().getResources().getString(R.string.host);
        HttpPost httppost = new HttpPost(host + php);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }
}

和PHP:

<?php
mysql_connect("localhost","root","");
mysql_select_db("dbBaggies");
$q=mysql_query("SELECT count(*) as 'return' FROM users
WHERE name='$_POST[user]' AND password ='$_POST[pass]'");
while($e=mysql_fetch_assoc($q))
{
    $output[]=$e;
} 
print(json_encode($output));
mysql_close();
?>

我也遇到了POST请求的问题,需要将图像上传到我的外部LAMP服务器。这真的帮助了我:

http://moazzam-khan.com/blog/?p=490

它是一个包含几个GET和POST方法的类。尝试使用这个:

public static HttpData post(String sUrl, Hashtable<String, String> ht) throws Exception {
            String key;
            StringBuffer data = new StringBuffer();
            Enumeration<String> keys = ht.keys();
            while (keys.hasMoreElements()) {
                    key = keys.nextElement();
                    data.append(URLEncoder.encode(key, "UTF-8"));
                    data.append("=");
                    data.append(URLEncoder.encode(ht.get(key), "UTF-8"));
                    data.append("&amp;");
            }
            return HttpRequest.post(sUrl, data.toString());
    }

我想你需要一些外部课程。请检查链接。