简体   繁体   English

无法从Apache向Django webserver发出http请求

[英]Can't issue http requests from Android to Django webserver with Apache

I'm trying to issue a post request to my django webserver (which behaves correctly when accessed via browser) 我正在尝试向我的django网络服务器发出一个帖子请求(通过浏览器访问时表现正常)

my post code: 我的邮政编码:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://23.23.237.174/save-item");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("user_email", WeShouldActivity.ACCOUNT_NAME));
        for (Field f : mData.keySet()) {
            nameValuePairs.add(new BasicNameValuePair(f.getName(), mData.get(f)));
        }
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        Log.v("GETREFERRALSSERVICE", "backing up items");

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        Log.v("GETREFERRALSSERVICE", e.getMessage());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.v("GETREFERRALSSERVICE", e.getMessage());
    }

my complete error in apache2/access.log (nothing shows up in error.log) 我在apache2 / access.log中的完整错误(error.log中没有显示任何内容)

174.253.199.12 - - [16/May/2012:03:25:15 +0000] "POST /save-item HTTP/1.1" 500 53055 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"

Does anyone have any idea what this might be/how to fix? 有谁知道这可能是什么/如何解决?

NOTE: this IS an error. 注意:这是一个错误。 The request is not getting through to my view, where it does with the exact same url in a browser. 请求没有通过我的视图,它在浏览器中使用完全相同的URL。 I'm printing out the request first thing in my view and nothing shows up in error.log 我在我的视图中首先打印出请求,并且在error.log中没有显示任何内容

'Apache-HttpClient/UNAVAILABLE (java 1.4)' is just the Android user agent string, not an error message. 'Apache-HttpClient / UNAVAILABLE(java 1.4)'只是Android用户代理字符串,而不是错误消息。 Something is wrong on the server side, you should try to debug it by adding logging, etc. Does the server side require authentication? 服务器端有问题,您应该尝试通过添加日志记录来调试它。服务器端是否需要身份验证? If so, you might be missing a session cookie, etc. 如果是这样,您可能会错过会话cookie等。

BTW, since it seems you are adding more than two items, there is really no point in using new ArrayList<NameValuePair>(2) just use the default constructor. 顺便说一句,因为你似乎要添加两个以上的项目,所以使用新的ArrayList<NameValuePair>(2)只是使用默认的构造函数是没有意义的。

Try this: 尝试这个:

HttpClient client = new DefaultHttpClient();    
client.getParams().setBooleanParameter("http.protocol.expect-continue", false);    

This solved my problem. 这解决了我的问题。

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

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