java.net.HttpURLConnection检查/测试连接,只有POST方法可用 [英] java.net.HttpURLConnection Check / TEST Connection while only POST Method is available

查看:143
本文介绍了java.net.HttpURLConnection检查/测试连接,只有POST方法可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java代码中调用的其他服务器上有一个web服务。它只响应POST方法(没有GET方法)可用。当我使用请求XML实现完整方法并调用Webservice时,此请求/响应正常工作,即它正常工作。

There is a webservice on some other server which I am calling in my Java Code. It responses only for POST method (there is NO GET method) available. This request / response works correctly while I implement the full method with request XML and call the Webservice, i.e. it works correctly.

但要求是 - >将有三个网络服务URL(可由管理员用户的我的应用程序配置)。首先,我必须检查第一个URL是否是连接。如果没有连接,那么去第二个连接...然后第三个连接。

But requirement is --> there will be three webservice URLs (configurable from my Application by Admin User). First, I have to check whether the first URL one is connection or not. If Not connecting, then go for second connection...then 3rd one.

我使用下面的代码,但一次又一次它给了我

I am using the below code , but again and again it is giving me


{responseCode = 500}
而不是
{responseCode = 200}

请告诉我怎么样我可以检查连接是否已建立?

Kindly Suggest me how can I check the connection is established or not ?

/* Start : Surajit Biswas (25-NOV-2015) : test if Alcatel WS is connection or not*/
    String wsURL = "https://hostservername:postnumber";
            String wsUserName = "someUserName";
            String wsPassword = "somePassword";
            String requestXML = "<soapenv:Envelope ...."; /* As I told, not required here*/  
            try{
                String authString = wsUserName+":"+wsPassword;
                byte[] byteAuthStr = authString.getBytes();
                String authBase64Str = Base64.encode(byteAuthStr);
                System.out.println(authBase64Str);
            URL url = new URL(wsURL);
            URLConnection  conn =  url.openConnection();
            HttpURLConnection connection = (HttpURLConnection)conn;
            connection.setDoOutput(true); 
            /*connection.setRequestMethod("GET");
             connection.setRequestMethod("POST");*/     connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            connection.setRequestProperty("Authorization", "Basic "+authBase64Str);
            connection.connect();
           System.out.println( connection.getResponseCode());
           boolean connected = false;
           switch (connection.getResponseCode()) {
           case HttpURLConnection.HTTP_OK:
               System.out.println(url + " **OK**");
               connected = true;
               break; // fine, go on
           case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
               System.out.println(url + " **gateway timeout**");
               break;// retry
           case HttpURLConnection.HTTP_UNAVAILABLE:
               System.out.println(url + "**unavailable**");
               break;// retry, server is unstable
           default:
               System.out.println(url + " **unknown response code**.");
               break ; // abort
       }
    }catch(Exception ex){
                System.err.println("Error creating HTTP connection");
                System.out.println(ex.getMessage());
            }
        }
/* End : Surajit Biswas (25-NOV-2015) : test if Alcatel WS is connection or not*/

在此先感谢您的帮助...
Surajit Biswas

Thanks in advance for your help... Surajit Biswas

推荐答案

实现您的要求的方法只是尝试依次发布到每个URL并在获得成功后立即停止。

The way to implement your requirement is simply to try to post to each URL in turn and stop as soon as you get a success.

什么你在这里尝试等于试图预测未来。它不会起作用。它不能工作。

What you're attempting here is tantamount to trying to predict the future. It won't work. It can't work.

这篇关于java.net.HttpURLConnection检查/测试连接,只有POST方法可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆