【问题标题】:Android httpPost(URL) -> 403 ForbiddenAndroid httpPost(URL) -> 403 Forbidden
【发布时间】:2014-09-03 20:39:23
【问题描述】:

我正在开发一个 Android 应用程序,该应用程序通过 new 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);

有人可以帮帮我吗?

提前致谢

【问题讨论】:

  • 嗨,如果您真的想得到好的答案,请显示您发布的代码。你希望人们如何帮助你?
  • 完成!我刚刚添加了一些代码
  • 使用 Chrome 浏览器开发者工具,尝试对该 URL 进行 POST。是不是也有错误?
  • @alpinescrambler 我刚刚用hurl.it 尝试了一个 POST,它给了我一个 200 状态,没有错误

标签: php android mysql http-status-code-403


【解决方案1】:

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

 //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;
}

【讨论】:

    【解决方案2】:

    也许您可以尝试发布 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);
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        如果 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");
        

        【讨论】:

          猜你喜欢
          • 2013-07-05
          • 2015-03-07
          • 2022-11-21
          • 2016-06-15
          • 2015-12-05
          • 2021-08-16
          • 2018-07-23
          • 2015-04-03
          相关资源
          最近更新 更多