如何使用RestSharp来向API发出登录名和密码? [英] How do I use RestSharp to POST a login and password to an API?

查看:3300
本文介绍了如何使用RestSharp来向API发出登录名和密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个C#应用程序,将调用api的Pacer.gov。首先,我需要做一个cookie来传递我的请求。我正在使用Chrome的Postman应用来尝试生成POST请求。有人可以解释我做错了什么吗?



我从Pacer.gov获取身份验证令牌的说明是


对于凭据,您可以在此处将用户名(loginid)和密码(passwd)发送到身份验证服务



我不知道从响应中期望什么,但我知道,在Pacer的电子邮件中提到了NextGenCSO cookie。如果我通过网络浏览器登录并查看cookies,我看到一个NextGenCSO cookie;我猜我应该在API调用的响应中看到类似的东西:





UPDATE - 6-3-16



感谢@ jonathon-reinhart的帮助,使用此代码:

  //身份验证
var client = new RestClient(https://pacer.login。 uscourts.gov);

client.CookieContainer = new System.Net.CookieContainer();
var request = new RestRequest(/ cgi-bin / check-pacer-passwd.pl,Method.POST);
request.AddParameter(loginid,@@@@@);
request.AddParameter(passwd,*****);

IRestResponse response = client.Execute(request);

var PacerSession =;

foreach(RestResponseCookie cookie in response.Cookies)
{
if(cookie.Name ==PacerSession)
{
PacerSession = cookie。值;
}
}


解决方案



  https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl?loginid=@@@@@&passwd=***** 

到地址栏不是POST。这是一个带有查询字符串参数的GET。



请参阅 RestSharp首页关于如何POST的示例:

  var request = new RestRequest(resource / {id},Method.POST ); 
request.AddParameter(name,value); //添加到基于方法的POST或URL查询字符串

您要包括 loginid passwd 作为数据(RestSharp调用的参数),不是

所以你应该这样做:

  var client = new RestClient (https://pacer.login.uscourts.gov); 

var request = new RestRequest(/ cgi-bin / check-pacer-passwd.pl,Method.POST);
request.AddParameter(loginid,@@@@);
request.AddParameter(passwd,****);

IRestResponse response = client.Execute(request);


I'm trying to write a C# application that will call an api for Pacer.gov. First thing I need to do it get a cookie to pass with my request. I'm using Chrome's Postman app to try to generate a POST request. Can someone explain what I'm doing wrong?

The instructions I got from Pacer.gov to get the authentication token are

For credentials, you can POST username(loginid) and password(passwd) to the authentication service here https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl This will issue you a PacerSession cookie.

Here are some ways I tried: (I’m just showing @@@@@ as the login and ***** as the password, but I used the actual login and password when I tried it)

  • Including username and password in the URL

    var client = new RestClient("https://pacer.login.uscourts.gov/cgi-in/check-pacer-passwd.pl?loginid=@@@@@&passwd=*****");
    var request = new RestRequest(Method.POST);
    request.AddHeader("postman-token", "a5bd0d9c-85f5-4ed6-6ad8-ac06e553f5db");
    request.AddHeader("cache-control", "no-cache");
    IRestResponse response = client.Execute(request);
    

    o Received HTML page saying "Access to PACER requires a valid account id and password. "

  • Including username and password in the header

    var client = new RestClient("https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl");
    var request = new RestRequest(Method.POST);
    request.AddHeader("postman-token", "add9dedf-10bf-eab4-428a-134c0e8a1783");
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("passwd", "*****");
    request.AddHeader("loginid", "@@@@@");
    IRestResponse response = client.Execute(request);
    

    o Received HTML page saying "Access to PACER requires a valid account id and password. "

  • Including username and password in the body as form data

    var client = new RestClient("https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl");
    var request = new RestRequest(Method.POST);
    request.AddHeader("postman-token", "454a8ebd-b2b4-3db2-0c5c-c48e2964c671");
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001");
    request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"loginid\"\r\n\r\n@@@@@\r\n-----011000010111000001101001\r\nContent-Disposition: form-data;          name=\"passwd\"\r\n\r\n*****\r\n-----011000010111000001101001--", ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);
    

    o Received message "Could not get any response"

  • Typing URL into Chrome with the username and password

    https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl?loginid=@@@@@&passwd=*****
    

    o Received error "This site can’t be reached"

UPDATE - 5-31-16

I've made several more attempts, but am still not getting the cookie back. I tried this code:

        var client = new RestClient("https://pacer.login.uscourts.gov");

        var request = new RestRequest("/cgi-bin/check-pacer-passwd.pl", Method.POST);
        request.AddParameter("loginid", "@@@@@");
        request.AddParameter("passwd", "*****");

        IRestResponse response = client.Execute(request);

When I look at the response, there is one cookie called JESSIONID which appears just to be a generic browser session ID, not the authentication code I'm expecting:

I'm not exactly sure what to expect from the response, but I know that a "NextGenCSO cookie" was mentioned in one of the e-mails from Pacer. If I login through the web browser and look at the cookies, I see a NextGenCSO cookie; I'm guessing I should be seeing something similar in the response from the API call:

UPDATE - 6-3-16

Thanks to help from @jonathon-reinhart, I was able to get the authentication cookie working with this code:

// Authentication
        var client = new RestClient("https://pacer.login.uscourts.gov");

        client.CookieContainer = new System.Net.CookieContainer();
        var request = new RestRequest("/cgi-bin/check-pacer-passwd.pl", Method.POST);
        request.AddParameter("loginid", "@@@@@");
        request.AddParameter("passwd", "*****");

        IRestResponse response = client.Execute(request);

        var PacerSession = "";

        foreach (RestResponseCookie cookie in response.Cookies)
        {
            if (cookie.Name == "PacerSession")
            {
                PacerSession = cookie.Value;
            }
        }

解决方案

You are not correctly POSTing as the directions indicate.

Putting this:

https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl?loginid=@@@@@&passwd=*****

into the address bar is not a POST. That's a GET with query string parameters.

See the RestSharp homepage for an example on how to POST:

var request = new RestRequest("resource/{id}", Method.POST);
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method

You want to include loginid and passwd as data ("parameters" as RestSharp calls them), not has headers.

So you should be instead doing something like this:

var client = new RestClient("https://pacer.login.uscourts.gov");

var request = new RestRequest("/cgi-bin/check-pacer-passwd.pl", Method.POST);
request.AddParameter("loginid", "@@@@");
request.AddParameter("passwd", "****");

IRestResponse response = client.Execute(request);

这篇关于如何使用RestSharp来向API发出登录名和密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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