0

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();

    }
        }
    }
Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
mehdi
  • 47
  • 3
  • 10
  • 1
    Surely that should be a `POST`? – RamblinRose Mar 08 '20 at 19:03
  • Does this answer your question? [How to upload a file and JSON data in Postman?](https://stackoverflow.com/questions/39037049/how-to-upload-a-file-and-json-data-in-postman) – RamblinRose Mar 08 '20 at 19:03
  • 2
    *"i get the above mentioned error"* HTTP 302 is not an error, it is a **redirect**, i.e. it is the server telling you to perform another GET using a different URL. – Andreas Mar 08 '20 at 19:09
  • yes but i used the same url in postman and didn't get this redirection, but 200 – mehdi Mar 08 '20 at 19:18
  • @RamblinRose, yes i corrected the post i wanted to say getting a file not posting – mehdi Mar 08 '20 at 19:25
  • 1
    Maybe PostMan follows redirects? In any case, use a modern HTTP library like Apache HTTPClient or OKHttp – OneCricketeer Mar 11 '20 at 08:37

0 Answers0