尝试将 XML 发布到 HTTPS URL 时出现 HTTP 403 服务错误

发布于 2024-12-11 23:59:07 字数 3303 浏览 0 评论 0原文

我正在尝试使用 Apache HttpClient 库编写一个小类,该类将向指定的 URL 发送 HTTPS 请求,发送一些 XML。当我运行代码时,我收到的 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。我认为这可能是一个证书问题,尝试添加一些代码来信任任何证书,似乎也不起作用,尽管我可能做错了。任何帮助表示赞赏。

I am trying to write a small class using the Apache HttpClient library that would do an HTTPS post to a specified URL sending some XML. When I run my code, the HTTP status line I receive back is "403 Service Error". Here's the complete error HTML returned:

$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

This is the code I am using:

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

Whatever XML I pass in doesn't seem to matter, it gives the same error no matter what. Note that this actually works with some URLs. For example, if I put https://www.facebook.com, it goes through. However, it doesn't work for my specified URL. I thought it might be a certificate issue, tried to add some code to trust any certificate, didn't seem to work either, though I may have done it wrong. Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如痴如狂 2024-12-18 23:59:07

Based on the SocketTimeoutException in the first line of the response HTML, I'm guessing that the component which implements the handler for the URL to which you are posting is having some connection problems to a source system it needs to generate the response data.

Basically, it looks like the problem is on the server, not your client.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文