简体   繁体   English

发出https发布请求

[英]make https post request

I am making an http post request to one of the xml gateways, but as per their rule I must post https post request, here is my code and i am getting a custom error code which indicates in their manual that the post request must be https, can you help me modifying the following code. 我向一个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();
        }

    }
}

As you are implementing the HttpClient you will need to add make sure that you can get signed certificates from the server. 在实现HttpClient时,您需要添加以确保可以从服务器获取签名证书。

see http://hc.apache.org/httpclient-3.x/sslguide.html and maybe the Customizing SSL in HttpClient section. 请参见http://hc.apache.org/httpclient-3.x/sslguide.html以及“ HttpClient中的自定义SSL”部分。

As a validation check make sure that access is OK via a browser first. 作为验证检查,请确保首先通过浏览器可以进行访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM