如何通过Pacer.gov API提出案例请求? [英] How do I make a case request from the Pacer.gov API?

查看:87
本文介绍了如何通过Pacer.gov API提出案例请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向名为Pacer.gov的API发出请求.我希望返回一个文件,但没有收到.有人可以帮我解决我所缺少的吗?

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?

所以我的C#Rest调用看起来像这样:

So my C# Rest call looks like this:

(变量PacerSession是我得到的身份验证cookie(在@ jonathon-reinhart的帮助下);在此了解更多信息:

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

如果我只输入URL https://pcl .uscourts.gov/dquery?download = 1& dl_fmt = xml& party = Moncrief 到Chrome中,我得到了一个XML文件.当我查看IRestResponse时,看不到任何看起来像文件的东西.我的请求有问题吗?还是我找回了文件,只需要知道如何检索它?

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?

如果我直接在浏览器中使用URL,这是文件的一部分:

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

这是我调试VS并查看IRestResponse变量时在VS中看到的内容:

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

更新-16/6/3

收到Pacer技术支持的回复:

Received this response from Pacer tech support:

在Advanced REST Client中,您将看到HTTP 302响应(重定向到另一个页面).在普通浏览器中,将自动执行重定向,而用户不会看到任何内容(即使在浏览器中的URL上也是如此). ARC不会自动跟随该重定向到目标页面. 您可以在响应的标题中看到包含结果的目标URL.
如果您手动将此URL作为HTTP GET请求剪切并粘贴到ARC,则将获得XML结果.我从未使用过C#,但是通常有一个与Web客户端关联的属性,它将强制客户端遵循重定向.

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.

我尝试添加此内容:

client.FollowRedirects = true;

但是在调试此代码时我仍然看不到xml文件:

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

IRestResponse response = client.Execute(request);

如何获取文件?我需要做些什么才能从要重定向到的URL中获取文件?

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?

推荐答案

您的代码存在一个主要问题.您只会携带返回的三个cookie中的一个 .您需要保留所有三个.以下代码是对此的一种可能的实现,后面有一些注释.

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;
    }
}

在使用Pacer的整个过程中,最简单的方法就是保持CookieContainer.我将功能包装到一个类中,只是为了使其更容易打包此答案,但是您可以根据需要实现它.我没有进行任何真正的错误检查,因此您可能想检查response.ResponseUri实际上是搜索页面而不是登录页面,并且内容实际上是格式正确的XML.

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.

我已经使用自己的Pacer帐户对此进行了测试,如下所示:

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

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

这篇关于如何通过Pacer.gov API提出案例请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆