简体   繁体   中英

Call REST service url in Java with DOMAIN(+ username+password) Authentication

I am trying to get Response from a REST service URL, nut getting Error: 401: Invalid username or password.

I am able to hit this URL in browser and get response with the provided credentials, as well as in SoapUI tool. I am providing the Domain value in the Authentication section while hitting the URL from SoapUI, without which I am getting Authentication error.

But from java I am unable to get the response.

If anyone can please help

public class Test_Rest_WS {

public static void main(String[] args) {

    System.setProperty("https.protocols", "TLSv1.2");
    System.setProperty("http.proxyHost","omaproxy.1dc.com");
    System.setProperty("http.proxyPort", "8080");
    ClientConfig clientConfig = new DefaultClientConfig();


    //clientConfig.getFeatures().put( JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);

    Client client = Client.create(clientConfig); 


        final HTTPBasicAuthFilter authFilter = new HTTPBasicAuthFilter("mysuer", "mypassword");
        client.addFilter(authFilter);
        client.addFilter(new LoggingFilter());



    WebResource webResource = client
            .resource("https://myurl");


      ClientResponse resp = webResource.accept("application/json")
      .type("application/json").get(ClientResponse.class);

    if(resp.getStatus() != 200){
        System.err.println("Unable to connect to the server");
    }
    String output = resp.getEntity(String.class);
    System.out.println("response: "+output);
}

}

Getting this error with the above code: 401: Invalid username or password

Expected output: The response xml.

Hi vikram,

  check your username it may be wrong like myuser

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