【发布时间】:2016-07-06 18:14:51
【问题描述】:
我正在使用一个硬件设备并想对其进行一些操作。
目前它使用 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) 现在我必须运行第二个命令来执行主要操作(在这种情况下为创建用户)
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 文件来创建用户。在这个命令中U1、P1、M1、C1、R1是参数。
谁能帮我从 ASP.NET 做同样的事情
问候 桑塔努
编辑:
根据给出的答案,我在 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
【问题讨论】: