繁体   English   中英

Android httpPost(URL) -> 403 Forbidden

[英]Android httpPost(URL) -> 403 Forbidden

我正在开发一个 Android 应用程序,它通过新的 HttpPost(url) 将数据发送到服务器(托管在 hosts.es - 免费测试应用程序); 然而,当我在手机中运行该应用程序时,我总是在我的日志中得到以下答案:

09-03 15:24:40.916: response: 
<html>
<head>
<title>Error 403 - Forbidden</title>
<meta http-equiv="Refresh" content="0;url=http://www.hostinger.co/error_404?" />
</head>
<body>
</body>
</html>

我的服务器代码是 php/mysql。 我已经更改了我的文件夹和文件 .php 在 FileZilla 中的权限,但仍然得到相同的答案。

我的客户的请求代码:

private static final String url = "http://yyy.esy.es/XXXX/process.php/senddata/";
// yyy subdomain in hostinger
// XXXX the folder where my php files are

protected void sendDataToServer(final TestData data) {
    ArrayList<NameValuePair> keyValuePairs = new ArrayList<NameValuePair>();
    keyValuePairs.add(new KeyValuePair(Constant.FB_ID, data.getId()));

    ....
    ResponseHandler<String> res = new BasicResponseHandler();
    HttpPost postMethod = new HttpPost(url);

    // Data that is to be sent
    postMethod.setEntity(new UrlEncodedFormEntity(keyValuePairs));

    // Execute HTTP Post Request
    String response = httpClient.execute(postMethod, res);

有人可以帮我吗?

提前致谢

即使使用以下配置来处理证书,我也希望可以帮助您。 但是,我将尝试为您提供简化版本,并提到它未经测试。 有证书的那个已经过测试和使用,所以它可以工作
如果您有任何评论,请说出来。

 //Method for getting the http client
 protected synchronized HttpClient getHttpClient() throws Exception {

    HttpClient client = null;
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setContentCharset(params,
            HTTP.DEFAULT_CONTENT_CHARSET);
    HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);// (params,
                                                                // TIMEOUT);
    ConnPerRoute connPerRoute = new ConnPerRouteBean(MAX_CONN_PER_ROUTE);
    ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
    ConnManagerParams.setMaxTotalConnections(params, 20);
    // registers schemes for http
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme(Protocol.HTTP.toString(),
            PlainSocketFactory.getSocketFactory(), 80));
    ClientConnectionManager connManager = new ThreadSafeClientConnManager(
            params, schemeRegistry);
    client = new DefaultHttpClient(connManager, params);
    return client;
}

//methods for executing the post. contentURL is the url that you use for post.  
protected HttpEntity executePostRequest(String contentURL, StringEntity body) {
    HttpEntity entity = null;
    HttpPost httpPost = new HttpPost(contentURL);

    httpPost.setEntity(body);
    httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
    HttpResponse response = executeHttpPost(httpPost);
    if (response != null) {
        entity = response.getEntity();
    }
    return entity;
}
 private synchronized HttpResponse executeHttpPost(HttpUriRequest httpPost) {
    Exception exception = null;
    try {
        HttpClient request = getHttpClient();
        HttpResponse response = request.execute(httpPost);
        handleStatusCode(response);
    } catch (Exception e) {
        Log.e(TAG, "error in executing the post request");
        if (e instanceof HttpResponseException) {
            Log.e(TAG, "HttpResponseException");
            if (this.HTTP_STATUS_CODE == HTTP_STATUS_UNAUTHORIZED) {
                exception = new AuthenticationException(
                        "Unauthorized Exception");
            } else {
                exception = new HttpException(
                        "Http not authorized Exception");
            }
        } else if (this.HTTP_STATUS_CODE == HTTP_STATUS_BAD_REQUEST) {
            Log.e(TAG, "BAD Request. error in get execute");
            exception = new Exception("Bad Request");
        }
    }
    if (exception != null) {
        Log.e(TAG, "Exception." + exception.getMessage());
    }
    return null;
}

也许您可以尝试发布 JSON 样式,看看会发生什么? 例如:

 protected void sendDataToServer(final TestData data) { throws Exception 
 {

   Map<String,String> params = new HashMap<String, String>();
   params.put(Constant.FB_ID, data.getId()); // both strings???


 DefaultHttpClient httpclient = new DefaultHttpClient();


 HttpPost httpost = new HttpPost(url);

 //convert the passed in parameters into JSON object
 JSONObject holder = getJsonObjectFromMap(params);


 StringEntity se = new StringEntity(holder.toString());


 httpost.setEntity(se);

 httpost.setHeader("Accept", "application/json");
 httpost.setHeader("Content-type", "application/json");


 ResponseHandler responseHandler = new BasicResponseHandler();
 httpclient.execute(httpost, responseHandler);
}

谢谢你的回答。 问题是托管商不允许远程连接到免费帐户中的数据库,我不知道。 为了解决这个问题,我已经改用其他免费的网络托管服务提供商,现在工作正常。

如果 Web 服务正在运行,并且在 Android Emulator 中,您会收到 403 错误。那么我认为您应该将 User-Agent 放在您的 httpconnection 代码中。 大多数情况下,您会在最新的 android 29/30 中遇到此类错误

connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");

暂无
暂无

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

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