Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Connection Refused

lakshminaraya11
Kilo Explorer

Hi , I am using PUT method to update the short_description from Java program. But it is giving "Connection to https://dev14659.service-now.com:443 refused" exception. Please help me in that.

16 REPLIES 16

I didn't change any roles of user . I am using the default user given by service now admin only, please let me know where we can check that roles?


package com.cg.servicenow;




import java.io.IOException;


import java.net.Authenticator;


import java.net.PasswordAuthentication;


import java.net.URL;


import java.net.URLConnection;




import org.apache.http.HttpEntity;


import org.apache.http.HttpException;


import org.apache.http.HttpHost;


import org.apache.http.auth.AuthScope;


import org.apache.http.auth.UsernamePasswordCredentials;


import org.apache.http.client.CredentialsProvider;


import org.apache.http.client.methods.CloseableHttpResponse;


import org.apache.http.client.methods.HttpPut;


import org.apache.http.entity.ByteArrayEntity;


import org.apache.http.impl.client.BasicCredentialsProvider;


import org.apache.http.impl.client.CloseableHttpClient;


import org.apache.http.impl.client.HttpClients;


import org.apache.http.util.EntityUtils;




import com.sun.jndi.ldap.Connection;



public class PutAction {


  public static void main(String[] args) throws IOException, HttpException {




  PutAction restAction = new PutAction();


  restAction.putRequest1();


  }



  public void putRequest() throws HttpException, IOException {


  // This must be valid json string with valid fields and values from table


  String postData = "{\"short_description\":\"Updated through Java Program \"}";


  CredentialsProvider credsProvider = new BasicCredentialsProvider();


                credsProvider.setCredentials(new AuthScope(new HttpHost("instance.service-now.com")),


                  new UsernamePasswordCredentials("admin", "Servicenow@123"));


                CloseableHttpClient httpclient = HttpClients.custom()


                                .setDefaultCredentialsProvider(credsProvider)


                                .build();



              try {


              HttpPut httpPut = new HttpPut("https://dev14659.service-now.com/api/now/table/sc_req_item/a4142d884f322200559c34a18110c712");


  httpPut.setHeader("Accept", "application/json");


  httpPut.setHeader("Content-Type", "application/json");


                HttpEntity entity = new ByteArrayEntity(postData.getBytes("utf-8"));


  httpPut.setEntity(entity);


                      System.out.println("Executing request " + httpPut.getRequestLine());


                      CloseableHttpResponse response = httpclient.execute(httpPut);


                      try {


                              System.out.println("----------------------------------------");


                              System.out.println(response.getStatusLine());


                              String responseBody = EntityUtils.toString(response.getEntity());


                              System.out.println(responseBody);


                      } finally {


                              response.close();


                      }


              } finally {


                    httpclient.close();


              }


  }


  public void putRequest1() throws HttpException, IOException {


  // This must be valid json string with valid fields and values from table


  String postData = "{\"short_description\":\"Updated through Java Program \"}";


  CredentialsProvider credsProvider = new BasicCredentialsProvider();


                credsProvider.setCredentials(


                                new AuthScope(new HttpHost("dev14659.service-now.com")),


                                new UsernamePasswordCredentials("admin", "Servicnow@123"));


                CloseableHttpClient httpclient = HttpClients.custom()


                                .setDefaultCredentialsProvider(credsProvider)


                                .build();


   


                try {


                  HttpPut httpPut = new HttpPut("https://dev14659.service-now.com/api/now/table/sc_req_item/a4142d884f322200559c34a18110c712");


  httpPut.setHeader("Accept", "application/json");


  httpPut.setHeader("Content-Type", "application/json");


                HttpEntity entity = new ByteArrayEntity(postData.getBytes("utf-8"));


  httpPut.setEntity(entity);


   


                        System.out.println("Executing request " + httpPut.getRequestLine());


                        CloseableHttpResponse response = httpclient.execute(httpPut);


                        try {


                                System.out.println("----------------------------------------");


                                System.out.println(response.getStatusLine());


                                String responseBody = EntityUtils.toString(response.getEntity());


                                System.out.println(responseBody);


                        } finally {


                                response.close();


                        }


                } finally {


                        httpclient.close();


                }


  }



}


I just did it from Postman on my dev instance and which worked fine: find_real_file.png


If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure

what is getting set in body of request?



whats here: System.out.println("Executing request " + httpPut.getRequestLine());


what is getting printed?


also tell me where are you setting authorization parameteres (type of authorization , user id , password)????


If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure

change this line from your code:


credsProvider.setCredentials(new AuthScope(new HttpHost("instance.service-now.com")),



to



credsProvider.setCredentials(new AuthScope(new HttpHost("https://dev14659.service-now.com")),


If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure