How do I make a case request from the Pacer.gov AP

2019-04-12 22:07发布

问题:

I am trying to make a request to an API called Pacer.gov. I'm expecting a file to be returned, but I'm not getting it. Can someone help me with what I'm missing?

So my C# Rest call looks like this:

(The variable PacerSession is the authentication cookie I got (with help from @jonathon-reinhart); read more about that here: How do I use RestSharp to POST a login and password to an API?)

            var client = new RestClient("https://pcl.uscourts.gov/dquery");

        client.CookieContainer = new System.Net.CookieContainer();
        //var request = new RestRequest("/dquery", Method.POST);
        var request = new RestRequest(Method.POST);
        request.AddParameter("download", "1");
        request.AddParameter("dl_fmt", "xml");
        request.AddParameter("party", "Moncrief");

        request.AddHeader("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36");
        request.AddHeader("content-type", "text/plain; charset=utf-8");
        request.AddHeader("accept", "*/*");
        request.AddHeader("accept-encoding", "gzip, deflate, sdch");
        request.AddHeader("accept-language", "en-US,en;q=0.8");
        request.AddHeader("cookie", "PacerSession=" + PacerSession);

        IRestResponse response = client.Execute(request);

If I just type the URL https://pcl.uscourts.gov/dquery?download=1&dl_fmt=xml&party=Moncrief into Chrome, I get back an XML file. When I look at the IRestResponse, I don't see anything that looks like a file. Is there something wrong with my request or am I getting the file back and just need to know how to retrieve it?

Here's part of the file I get back if I use the URL directly in the browser:

Here's what I see in VS when I debug it and look at the IRestResponse variable:

UPDATE - 6/3/16

Received this response from Pacer tech support:

In the Advanced REST Client, you will see a HTTP 302 response (a redirect to another page). In a normal browser, the redirect is automatically followed without the user seeing anything (even on the URL in the browser). The ARC does not automatically follow that redirect to the target page. You can see in the header of the response the target URL that has the results.
If you manually cut and paste this URL to the ARC as a HTTP GET request, you will get the XML results. I have never used C#, but there is usually a property associated with web clients that will force the client to follow the redirect.

I tried adding this:

client.FollowRedirects = true;

but I'm still not seeing an xml file when I debug this code:

IRestResponse response = client.Execute(request);

How do I get the file? Is there something I have to do to get the file from the URL it's being redirected to?

回答1:

There's one major problem with your code. You're only carrying one of the three cookies that checp-pacer-passwd.pl returns. You need to preserve all three. The following code is a possible implementation of this, with some notes afterwards.

public class PacerClient
{
    private CookieContainer m_Cookies = new CookieContainer();
    public string Username { get; set; }
    public string Password { get; set; }
    public PacerClient(string username, string password)
    {
        this.Username = username;
        this.Password = password;
    }
    public void Connect()
    {
        var client = new RestClient("https://pacer.login.uscourts.gov");
        client.CookieContainer = this.m_Cookies;
        RestRequest request = new RestRequest("/cgi-bin/check-pacer-passwd.pl", Method.POST);
        request.AddParameter("loginid", this.Username);
        request.AddParameter("passwd", this.Password);

        IRestResponse response = client.Execute(request);

        if (response.Cookies.Count < 1)
        {
            throw new WebException("No cookies returned.");
        }
    }
    public XmlDocument SearchParty(string partyName)
    {
        string requestUri = $"/dquery?download=1&dl_fmt=xml&party={partyName}";

        var client = new RestClient("https://pcl.uscourts.gov");
        client.CookieContainer = this.m_Cookies;

        var request = new RestRequest(requestUri);

        IRestResponse response = client.Execute(request);

        if (!String.IsNullOrEmpty(response.Content))
        {
            XmlDocument result = new XmlDocument();
            result.LoadXml(response.Content);
            return result;
        }
        else return null;
    }
}

It's easiest to just keep a hold of the CookieContainer throughout the entire time you're working with Pacer. I wrapped the functionality into a class, just to make it a little easier to package up with this answer, but you can implement it however you want. I didn't put in any real error checking, so you probably want to check that response.ResponseUri is actually the search page and not the logon page, and that the content is actually well-formed XML.

I've tested this using my own Pacer account, like so:

PacerClient client = new PacerClient(Username, Password);
client.Connect();
var document = client.SearchParty("Moncrief");