I can access XML data using CURL -u and get it in the browser using HTTP Basic Auth. How do I duplicate the process in Java?

StackOverflow https://stackoverflow.com/questions/18306879

curl -u APIKEY https://api.recurly.com/v2/accounts

In the terminal... Works like a charm and gets the XML I want...

Update:

https://companyName.recurly.com/v2/accounts.xml Asks for a username and password to access the xml from a browser window. I can type this in and the browser will display the xml data for me. I just need a barebones walk through of something that gets the xml doc into my "System.out.println()" screen. I'll figure it out from there I just need to get the data on my screen!

有帮助吗?

解决方案 3

This is the answer I was looking for. I found it at this website... http://www.avajava.com/tutorials/lessons/how-do-i-connect-to-a-url-using-basic-authentication.html. I'm so excited I finally got this to work. This works like a charm just replace "companyName" and "APIKEY" with your info. Thanks to everybody and their input and help!

package getrecurly;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.codec.binary.Base64;

/**
 *
 * @author jackcrishjr
 */
public class GetRecurly{


public static void main(String[] args) {

        try {
            String webPage = "https://companyName.recurly.com/v2/accounts.xml";
            String name = "APIKEY";
            String password = "APIKEY";

            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);
            URLConnection urlConnection = url.openConnection();
            urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
            InputStream is = urlConnection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);

            int numCharsRead;
            char[] charArray = new char[1024];
            StringBuffer sb = new StringBuffer();
            while ((numCharsRead = isr.read(charArray)) > 0) {
                sb.append(charArray, 0, numCharsRead);
            }
            String result = sb.toString();

            System.out.println("*** BEGIN ***");
            System.out.println(result);
            System.out.println("*** END ***");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

其他提示

You can HttpClient in you java code

See the link

http://hc.apache.org/httpclient-3.x/

I would use HttpURLConnection to get it

    URL url = new URL(fileURL);
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

    System.out.println("Content-Type = " + httpConn.getContentType());
    System.out.println("Content-Disposition = " + httpConn.getHeaderField("Content-Disposition"));
    System.out.println("Content-Length = " + httpConn.getContentLength());

   InputStream inputStream = httpConn.getInputStream();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top