【问题标题】:HTTP 403 Service Error when trying to post XML to HTTPS URL尝试将 XML 发布到 HTTPS URL 时出现 HTTP 403 服务错误
【发布时间】:2011-10-25 14:27:49
【问题描述】:

我正在尝试使用 Apache HttpClient 库编写一个小类,该库将向指定 URL 发送一些 XML 的 HTTPS 发布。当我运行我的代码时,我收到的 HTTP 状态行是“403 服务错误”。这是返回的完整错误 HTML:

$errorDump  java.net.SocketTimeoutException:Read timed out
$errorInfo  
$errorDump  java.net.SocketTimeoutException:Read timed out
$error  Read timed out
$localizedError Read timed out
$errorType  java.net.SocketTimeoutException
$user   
$time   2011-10-25 09:39:29 EDT
$error  Read timed out
$errorType  java.net.SocketTimeoutException

这是我正在使用的代码:

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpXmlPost {

    public static void main(String[] args)
    {
        String url = "https://someurlhere.com";
        String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><xmlTag></xmlTag>";

        String content = request(xmlStr, url);
        System.out.println(content);
    }

    private static String request(String xmlStr, String url) {
        boolean success = false;
        String content = "";

        HttpClient httpclient = new DefaultHttpClient();

        try {
            HttpPost httpPost = new HttpPost(url.trim());

            InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(xmlStr.getBytes() ), -1);
            reqEntity.setContentType("application/xml");
            reqEntity.setChunked(true);

            httpPost.setEntity(reqEntity);

            System.out.println("Executing request " + httpPost.getRequestLine());
            HttpResponse response = httpclient.execute(httpPost);
            HttpEntity resEntity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if(response.getStatusLine().getStatusCode() == 200){
                success = true;
            }
            if (resEntity != null) {
                System.out.println("Response content length: " + resEntity.getContentLength());
                System.out.println("Chunked?: " + resEntity.isChunked());
            }

            BufferedReader reader = new BufferedReader(new InputStreamReader(resEntity.getContent()));
            StringBuilder buf = new StringBuilder();
            char[] cbuf = new char[ 2048 ];
            int num;

            while ( -1 != (num=reader.read( cbuf ))) {
                buf.append( cbuf, 0, num );
            }

            content = buf.toString();

            EntityUtils.consume(resEntity);
        } 
        catch (Exception e) {
            System.out.println(e);
        }
        finally {
            httpclient.getConnectionManager().shutdown();
        }

        return content;
    }
}

无论我传入什么 XML 似乎都无关紧要,无论如何它都会给出相同的错误。请注意,这实际上适用于某些 URL。例如,如果我输入https://www.facebook.com,它就会通过。但是,它不适用于我指定的 URL。我认为这可能是证书问题,试图添加一些代码来信任任何证书,但似乎也没有工作,尽管我可能做错了。任何帮助表示赞赏。

【问题讨论】:

    标签: java https httpclient


    【解决方案1】:

    基于响应 HTML 第一行中的 SocketTimeoutException,我猜测为您发布的 URL 实现处理程序的组件与它需要生成的源系统存在一些连接问题响应数据。

    基本上,问题似乎出在服务器上,而不是您的客户端上。

    【讨论】:

    • 奇怪的是,如果我尝试只访问浏览器中的链接,它就可以工作。当然,这是一个 POST 并且浏览器执行一个 GET,但它仍然很奇怪。为了提供更多背景知识,我基本上是在尝试将 ColdFusion 中的 CFHTTP 调用替换为这个 Java 调用,因为使用 ColdFusion,我不得不一直为某些 URL 将证书重新导入密钥库,这是不切实际的。使用 Java,我应该能够编写一些代码来忽略证书错误,但我什至没有达到证书错误的地步。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 2011-06-11
    • 1970-01-01
    • 2015-04-10
    • 2011-06-05
    • 1970-01-01
    相关资源
    最近更新 更多