Connection Refused
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2016 01:39 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2016 02:32 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2016 02:32 AM
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();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2016 02:39 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2016 02:43 AM
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)????
Regards,Sushant Malsure
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2016 02:46 AM
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")),
Regards,Sushant Malsure