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

在终端中...像魅力一样工作并获得我想要的 XML...

更新:

https://companyName.recurly.com/v2/accounts.xml 要求输入用户名和密码以从浏览器窗口访问 xml。我可以输入这个,浏览器将为我显示 xml 数据。我只需要一个准系统来完成将 xml 文档带入我的“System.out.println()”屏幕的内容。我会从那里弄清楚我只需要在我的屏幕上获取数据!

4

3 回答 3

1

你可以在你的java代码中使用HttpClient

见链接

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

于 2013-08-19T05:28:55.693 回答
1

我会使用 HttpURLConnection 来获取它

    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();
于 2013-08-19T05:32:03.980 回答
0

这是我一直在寻找的答案。我在这个网站上找到了它... http://www.avajava.com/tutorials/lessons/how-do-i-connect-to-a-url-using-basic-authentication.html。我很兴奋,我终于让它工作了。这就像一个魅力,只需用您的信息替换“companyName”和“APIKEY”。感谢大家以及他们的投入和帮助!

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();
        }
    }
}
于 2013-09-22T02:01:56.930 回答