HTTP :connect timeout and read timeout for URLStre

2020-03-30 08:53发布

I'm a beginner when it comes to HTTP connections. Currently i'm working with SAAJ api to have my soap based client, where to handle timeouts i ended up using URLStreamHandler for HTTP connection properties with the endpoints.

Problem is that this timeout works for my windows based system, however it isn't working for the Linux server it is going to go live on.

below is the code for fetching endpoint with set properties. It is a HTTP POST connection.

    URL endpoint = new URL (null, url, new URLStreamHandler () {
        protected URLConnection openConnection (URL url) throws IOException {
            // The url is the parent of this stream handler, so must create clone
            URL clone = new URL (url.toString ());
            HttpURLConnection connection = (HttpURLConnection) clone.openConnection();
            connection.setRequestProperty("Content-Type",
                    "text/xml");

            connection.setRequestProperty("Accept",
                    "application/soap+xml, text/*");

            // If we cast to HttpURLConnection, we can set redirects
            // connection.setInstanceFollowRedirects (false);
            connection.setDoOutput(true);
            connection.setConnectTimeout(3 * 1000);
            connection.setReadTimeout(3 * 1000);
            return connection;

for the SAAJ API part, below is the implementation, pretty basic one

    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory
            .newInstance();
    soapConnection = soapConnectionFactory.createConnection();

    is = new ByteArrayInputStream(command.getBytes());

    SOAPMessage request = MessageFactory.newInstance(
            SOAPConstants.SOAP_1_1_PROTOCOL).createMessage(
                    new MimeHeaders(), is);

    MimeHeaders headers = request.getMimeHeaders();
    headers.addHeader("Content-Type", "text/xml");

    request.saveChanges();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    request.writeTo(out);

    soapResponse = soapConnection.call(request, endpoint);

Is it that system properties would affect connect or read timeout. If so please let me know what could cause this behavior.

0条回答
登录 后发表回答