I'am trying get a text file from a server but i get the above mentioned error, someone has an idea how to solve it ?
when i perform a GET request with POSTMAN i get a 200 response.
and now stack overflow is saying "It looks like your post is mostly code; please add some more details." this is why i'm adding this line and hopefully this stupid algorithm will let me post my question in peace
UPDATE : even with the jenkins plugin "http request" i get a 200 message, does it mean that there is something wrong with my code ?
import java.io.DataInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.*;
import java.net.*;
import javax.net.ssl.HttpsURLConnection;
import java.lang.Object;
import org.apache.commons.codec.binary.Base64;
import javax.net.ssl.HttpsURLConnection;
public class httpClient {
public static void sender() {
try {
File file = new File("file.txt");
FileReader input = new FileReader(file);
BufferedReader bufRead = new BufferedReader(input);
String Line=null;
String[] array1= new String[5000];
int count=0;
while ( (myLine = bufRead.readLine()) != null)
{ count++;
array1[count] = Line;
}
String webPage = "URL";
String name = "usr";
String password = "pass";
String authString = name + ":" + password;
System.out.println("auth string: " + authString);
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
System.out.println("Base64 encoded auth string: " + authStringEnc);
URL url = new URL(webPage);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("Content-Type", "text/plain");
urlConnection.setRequestProperty("Accept", "text/plain");
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
////////////////////////////////////////////////////////
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int number;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
while ((number = isr.read(charArray)) > 0) {
sb.append(charArray, 0, number);
}
String result = sb.toString();
int code = urlConnection.getResponseCode();
System.out.println(code);
System.out.println(result);
}catch (IOException e) {
e.printStackTrace();
}
}
}