It's all about the answers!

Ask a question

Not able to fecth JRS data through java application


KT PD (597) | asked Jan 20 '22, 12:56 a.m.
edited Jan 20 '22, 4:21 a.m.

   Hi Team,


I have created one java application using jdk 1.8 and the logic is that , I am hitting one url and getting XML file as result. The URL is fetching the JRS report data into XML format. 
When I hit the URL through postman or through web it showing all expected result, but when I am passing through java application it is showing only header. 
Can you please help me how to fetch full data, Please find below code for reference.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream.GetField;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Base64;
import java.util.Scanner;

import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;  
import org.w3c.dom.NodeList;  
import org.w3c.dom.Node;  
import org.w3c.dom.Element;  

public class report {

public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException {
java.lang.System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom());
        SSLContext.setDefault(ctx);
        
Scanner sc= new Scanner(System.in); 
//System.out.print("Enter URL");
//String baseURL  =sc.next();
String baseURL = "URL";
String username = "";
String password = "";
String cookie= ";
InputStream in = null;
FileOutputStream out = null;
try {
//KeyStore ks = KeyStore.getInstance("Windows-ROOT");
//ks.load(null, null) ;
url = new URL(baseURL);
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setDoOutput(true);
  conn.setReadTimeout(300000);
  conn.setConnectTimeout(300000);
  conn.setUseCaches(false);
  conn.setAllowUserInteraction(false);
  conn.setRequestProperty("Content-Type", "application/json");
  conn.setRequestProperty("Accept-Charset", "UTF-8");
  conn.setRequestMethod("GET");
  String a = "";
System.out.println("Testing");
  String userCredentials = username.trim() + ":" + password.trim();
  String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
  conn.setRequestProperty ("Authorization", basicAuth);
  conn.setRequestProperty("Cookie", cookie);
  
    
   out = new FileOutputStream("myxml");
  int c;
  byte[] b = new byte[8192];
  while ((c = in.read(b)) != -1){
 
  out.write(b, 0, c);
  }
  
  sc.close();
  System.out.println("End");
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public InputStream makeHTTPRequest(String url1, String username, String password){
InputStream in = null;
try{
URL url = new URL(url1);
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setDoOutput(true);
  conn.setReadTimeout(300000);
  conn.setConnectTimeout(300000);
  conn.setUseCaches(false);
  conn.setAllowUserInteraction(false);
  conn.setRequestProperty("Content-Type", "application/json");
  conn.setRequestProperty("Accept-Charset", "UTF-8");
  conn.setRequestMethod("GET");
  
 
  String userCredentials = username.trim() + ":" + password.trim();
  String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
  conn.setRequestProperty ("Authorization", basicAuth);
  conn.setRequestProperty("Cookie", "");
  in = conn.getInputStream();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return in;
}
    private static class DefaultTrustManager implements X509TrustManager {

@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// TODO Auto-generated method stub
}

@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// TODO Auto-generated method stub
}

@Override
public X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
    }
}

One answer



permanent link
Ralph Schoon (63.1k33645) | answered Jan 20 '22, 2:47 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

 I don't think the forum is a place for on line debugging code, if you get the information with postman and you don't get it with how you use the Java HTTP client, it is most likely you are not authenticated or you do something else wrong.


https://jazz.net/wiki/bin/view/Main/NativeClientAuthentication describes how to authenticate. I do not know if basic auth always works. https://rsjazz.wordpress.com/2021/10/15/elm-authentication/ is my experience with trying to implement this.

If you get correct headers but not the rest of the response search for how to use your HTTP client of choice. Maybe search or ask on stackoverflow.com

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.