0

我正在向其中一个 xml 网关发出 http 发布请求,但根据他们的规则,我必须发布 https 发布请求,这是我的代码,我收到一个自定义错误代码,在他们的手册中表明发布请求必须是 https ,你能帮我修改下面的代码吗?

public class PostXML {

    public static void main(String args[]) throws FileNotFoundException {
        // Get target URL
        String strURL = "http://xmlgw.companieshouse.gov.uk/v1-0/xmlgw/Gateway" ;

        // Get file to be posted
        String strXMLFilename = "F:\\12-8\\CompanyFormation\\CompanyFormation\\web\\file.xml";
        File input = new File(strXMLFilename);

        // Prepare HTTP post
        PostMethod post = new PostMethod(strURL);

        // Request content will be retrieved directly
        // from the input stream
        // Per default, the request content needs to be buffered
        // in order to determine its length.
        // Request body buffering can be avoided when
        // content length is explicitly specified
        post.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(input), input.length()));

        // Specify content type and encoding
        // If content encoding is not explicitly specified
        // ISO-8859-1 is assumed
        post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");

        // Get HTTP client
        HttpClient httpclient = new HttpClient();

        // Execute request
        try {
            int result = httpclient.executeMethod(post);

            // Display status code
            System.out.println("Response status code: " + result);

            // Display response
            System.out.println("Response body: ");
            System.out.println(post.getResponseBodyAsString());


        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            // Release current connection to the connection pool 
            // once you are done
            post.releaseConnection();
        }

    }
}
4

1 回答 1

0

在实现 HttpClient 时,您需要添加确保可以从服务器获取签名证书。

请参阅http://hc.apache.org/httpclient-3.x/sslguide.html以及可能在 HttpClient 部分自定义 SSL。

作为验证检查,请确保首先通过浏览器进行访问。

于 2013-09-09T07:08:44.033 回答