2014-09-03 63 views
0

我正在开发一个Android应用程序,通过新的HttpPost(url)向服务器发送数据(托管在hostinger.es中 - 免费测试应用程序); howevere,当我在我的手机上运行应用程序,我总是在我的日志如下回答:Android httpPost(URL) - > 403 Forbidden

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); 

有人能帮助我吗?

在此先感谢

+0

嗨,如果你真的想得到很好的答案,请展示te帖子的代码。你希望人们如何帮助你? – 2014-09-03 20:56:29

+0

完成!我刚刚添加了一些代码 – 2014-09-03 21:08:53

+0

使用Chrome浏览器开发人员工具,尝试在该URL上执行POST。是否也有错误? – alpinescrambler 2014-09-03 22:16:23

回答

0

感谢您的回答。问题是hostinger不允许远程连接到数据库的免费帐户,我不知道。为了解决这个问题,我已经更换为其他免费的虚拟主机提供商,现在它工作正常。

0

我希望我可以帮你,即使下面的配置用来对付证书。不过,我会尽力为您提供简化版本,并提及它未经测试。具有证书的那个被测试和使用,所以它的工作
如果你有任何评论就说出来。

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

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