如何在ASP.NET文件中使用cURL

时间:2016-03-20 05:54:13

标签: asp.net curl cookies

我正在使用硬件设备并希望对其进行一些操作。

目前它使用cURL来执行操作。但我想在ASP.NET Web表单应用程序中执行相同操作,下面给出了设备使用的CURL命令:

1)首先,我必须将用户身份验证存储在cookie文件中:

curl -1 -v -L -k -c cookie.txt  -d save=2 -d adm_name=admin -d adm_pwd=admin  https://10.71.1.111/check.shtml

这个商店cookie.txt在我的文件夹中。

2)现在我必须运行第二个命令来执行主操作(在本例中为Create User)

curl -1 -v -L -k -b cookie.txt -d save=1 -d U1=michael -d P1=abcd1234 -d M1=00:09:6B:CD:88:00 -d C1=d -d R1=FinanceDep https:// 10.71.1.111/UserAuthentication/AddUser.shtml 

这里我发送cookie.txt文件来创建用户。在此命令中U1P1M1C1R1是参数。

任何人都可以帮助我从ASP.NET中做同样的事情

此致 Santanu

修改

根据给出的答案,我在VB.NET中转换了代码并使用了它,但不幸的是它也没有给出任何错误并在回复中返回任何内容

Dim jar As New CookieContainer()

        Dim target As New Uri("https://x.x.x.x/check.shtml")

        'jar.Add(New Cookie("adm", "admin"))
        'jar.Add(New Cookie("admpwd", "6e49b5b6c16eff37c0d8217382fe1c7e"))
        'jar.Add(New Cookie("first_login_home_page", "home.shtml"))
        'jar.Add(New Cookie("name", "value"))

        jar.Add(New Uri("https://x.x.x.x/check.shtml"), New Cookie("adm", "admin"))
        jar.Add(New Uri("https://x.x.x.x/check.shtml"), New Cookie("admpwd", "6e49b5b6c16eff37c0d8217382fe1c7e"))
        jar.Add(New Uri("https://x.x.x.x/check.shtml"), New Cookie("first_login_home_page", "home.shtml"))

        Dim request As HttpWebRequest = DirectCast(WebRequest.Create("https://x.x.x.x/UserAuthentication/AddUser.shtml"), HttpWebRequest)

        request.CookieContainer = jar
        request.AllowAutoRedirect = True
        ' request.ServerCertificateValidationCallback = AddressOf AcceptAllCertifications

        ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(Function() True)


        request.Method = "POST"

        Dim fields As New NameValueCollection()
        fields.Add("save", "1")
        fields.Add("U1", "michael")
        fields.Add("P1", "abcd1234")
        fields.Add("C1", "1")
        fields.Add("R1", "FinanceDep")



        Using requestBody As Stream = request.GetRequestStream()
            Using wtr As New StreamWriter(requestBody, Encoding.ASCII)
                Dim first As [Boolean] = True
                For Each key As [String] In fields.Keys
                    If Not first Then
                        wtr.Write("&"c)
                    End If
                    Dim keyEnc As [String] = HttpUtility.UrlEncode(key)
                    Dim valEnc As [String] = HttpUtility.UrlEncode(fields(key))

                    wtr.Write(keyEnc)
                    wtr.Write("="c)
                    wtr.Write(valEnc)
                    first = False
                Next
            End Using
        End Using


        Dim response As HttpWebResponse = Nothing

        Try
            response = request.GetResponse()
        Catch wex As WebException
            If wex.Response IsNot Nothing Then
                response = wex.Response
            End If
        End Try

1 个答案:

答案 0 :(得分:0)

为了发出HTTP请求,.NET有三个选项:

  • HttpWebRequest - 一个带有阻止API的简单单一请求/响应。
  • HttpClient - 在.NET 4中引入,具有async API。
  • WebClient - 围绕HttpWebRequest的有状态包装器。我不建议使用它。

我个人的偏好是使用HttpWebRequest,因为我发现处理异步调用的情况下,阻塞调用很好,而且它的相对简单:

你的卷曲选项是:

-1     Force TLS
-v     Verbose
-L     Follow HTTP Location: header in redirections
-k     Disables TLS certificate CA checking
-c     Specify cookie jar file
-d     Specify request body form fields and use `application/x-www-form-urlencoded`

可以像这样转换为HttpWebRequest

CookieContainer jar = new CookieContainer();
jar.Add( new Cookie("name", "value") );

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://10.71.1.111/check.shtml");
request.CookieContainer = jar;
request.AllowAutoRedirect = true;
request.ServerCertificateValidationCallback += delegate(sender, certificate, chain, sslPolicyErrors) {
    return true; // ignore all certificate errors
};
request.Method = "POST";

NameValueCollection fields = new NameValueCollection();
fields.Add("save", "2");
fields.Add("adm_name", "admin");
fields.Add("adm_pwd", "admin");
fields.Add("U1", "michael");

// Set request body
using(Stream requestBody = request.GetRequestStream())
using(StreamWriter wtr = new StreamWriter( requestBody, Encoding.ASCII ) ) {
    Boolean first = true;
    foreach(String key in fields.Keys) {
        if( !first ) wtr.Write('&');
        String keyEnc = HttpUtility.UrlEncode( key );
        String valEnc = HttpUtility.UrlEncode( fields[key] );

        wtr.Write( keyEnc );
        wtr.Write('=');
        wtr.Write( valEnc );
        first = false;
    }
}

HttpWebResponse response = null;
try {
    response = request.GetResponse();
} catch(WebException wex) {
    if( wex.Response != null ) response = wex.Response;
}

// do stuff with response

请注意,-1选项不适用,因为HttpWebRequest将使用系统范围的TLS设置,并且因为它使用https://方案进行连接,所以它不会获胜回退到普通的HTTP。

我看到你正在使用文件系统中存在的cookie文件。您需要在jar变量中添加相同的内容。我提供了一个示例cookie(Name:Value)。

AllowAutoRedirect属性设置为true是不必要的,因为这是属性默认值。

最后,其余代码将获取每个密钥+名称对,并将URL编码到请求正文中。我同意这很复杂,我很惊讶HttpWebRequest没有一种简单的方法来生成URL编码的请求体。