简体   繁体   中英

How to check the status of POST endpoint/url in java

I have requirement to post a soap xml to a POST endpoint. But before posting, I need to do a health check of that url or find the status of that url. I tried to do an HttpURLConnection 'GET' method, but the url does not support 'GET'. Please help!

As you said yourself in the question that method is POST and not GET. So sending a GET request is irrelevant (regardless of whether it works or not). You can send a POST request using HttpURLConnection. But you will have to read and learn how to properly do it. The lazy way is to use a 3d party HttpClient. Here are a few options:

  1. Apache HttpClient - a very widely used library
  2. OK HttpClient - Open Source library
  3. And my favorite (Open Source library written by me) MgntUtils library

With MgntUtils library your code could be as simple as

private static void testHttpClient() {
    HttpClient client = new HttpClient();
    client.setContentType("application/json; charset=utf-8");
    client.setConnectionUrl("http://www.your.url.com/");
    String content = null;
    try {
        content = client.sendHttpRequest(HttpMethod.POST);
    } catch (IOException e) {
        content = TextUtils.getStacktrace(e, false);
    }
    System.out.println(content);
}

Here is Javadoc for MgntUtils HTTPClient class . The library itself could be found here as Maven artifacts or on Git (including sources and JavaDoc). An article about the library (although it doesn't describe HttpClient feature) could be found here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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