HttpPost,HttpClient目标服务器无法响应

时间:2013-11-20 12:20:09

标签: http-post apache-httpclient-4.x

我在发布时无法使用HttpClient连接到我的服务器,它可以正常使用get,并且可以正常发布到其他服务器(例如google),任何想法?

我不使用安卓。

服务器通过浏览器访问时响应正常。

由于它可能是服务器配置问题,我没有对服务器的完全访问权限,它是托管网络服务器,但我可以访问cpanel

private static String GetURL(String inUrl, String post) {
    String inputLine = "";
    try {
        if (!inUrl.contains("http")) {
            throw new Exception("Invalid URL");
        } else {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(inUrl);
            httpPost.addHeader("Accept-Charset", "UTF-8");
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");

            //create post
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("req", post));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = client.execute(httpPost);
            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            inputLine = in.readLine();
            in.close();

        }
    }catch (Exception ex) {
        inputLine = "" + Comms.ERROR_COULD_NOT_REACH_SERVER;
        Log.writeLog("Could Not Reach Server: \"" + inUrl + "\"");
        ex.printStackTrace();
    }

    return inputLine;
}


org.apache.http.NoHttpResponseException: The target server failed to respond
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:95)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:191)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:715)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:520)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
at Comms.GetURL(Comms.java:87)
at Comms.sendCommand(Comms.java:64)
at Comms.main(Comms.java:43)

0 个答案:

没有答案