来自 asp.net/vb 的卷发
Posted
技术标签:
【中文标题】来自 asp.net/vb 的卷发【英文标题】:Curl post from asp.net/vb 【发布时间】:2014-06-20 12:57:49 【问题描述】:我正在尝试使用以下 curl 命令并在我的 vb.net 页面中使用它来连接到第 3 方。
curl -X POST -i -H "Accept: application/json" -u someusername:somepassword "https://domain.com/xyz/api/user" -d 'firstName=john&lastName=smith&ssn=111223333¤tAddress=1 Main ST¤tCity=somecity¤tState=WA¤tZip=99999'
上面的命令在 cygwin 中工作正常,我尝试将其转换为跟随但没有运气。我尝试对密码进行编码,但这也无济于事。我将不胜感激。谢谢!
Dim url As String = "https://domain.com/xyz/api/user"
Dim data As String = """CurrentAddress"":""1 Main St"", ""CurrentCity"":""Somecity"",""CurrentState"":""WA"",""CurrentZip"":""99999"",""FirstName"":""john"",""LastName"":""smith"",""ssn"":""111223333"""
Dim myReq As WebRequest = WebRequest.Create(url)
myReq.Method = "POST"
myReq.ContentLength = data.Length
myReq.ContentType = "application/json"
myReq.Credentials = New NetworkCredential("someusername", "somepassword")
Using ds As Stream = myReq.GetRequestStream()
ds.Write(System.Text.ASCIIEncoding.Default.GetBytes(data), 0, data.Length)
End Using
Dim wr As WebResponse = myReq.GetResponse()
Dim receiveStream As Stream = wr.GetResponseStream()
Dim reader As New StreamReader(receiveStream, Encoding.UTF8)
Dim content As String = reader.ReadToEnd()
MsgBox(content)
【问题讨论】:
您的代码是否有错误,或者不是您期望的响应? 我认为我的问题是它没有正确地使用用户名/密码进行身份验证,因此服务器没有响应正确的数据。如果您认为这会有所帮助,我可以复制粘贴来自服务器的确切响应。有没有一种简单的方法可以查看服务器从这个请求中看到的内容? 有简单的方法吗? - 除非 - 听起来不像 - 你可以访问那篇文章。这可能会有所帮助,但我不确定。你从这里尝试过什么吗? --- ***.com/questions/5152723/… 【参考方案1】:搞砸了两天后,我能够让它工作。这是我想出的解决方案。
Dim paramName As String() = New String(6) "firstName", "lastName", "ssn", "currentAddress", "currentCity", "currentState", "currentZip"
Dim paramVal As String() = New String(6) "John", "Smith", "111223333", "1 main st", "any city", "dc", "11111"
Dim result As String
result = HttpPost("https://domain.com/XYZ/api/user", paramName, paramVal)
Private Shared Function HttpPost(url As String, paramName As String(), paramVal As String()) As String
Dim req As HttpWebRequest = TryCast(WebRequest.Create(New Uri(url)), HttpWebRequest)
req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded"
req.Headers("Authorization") = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("myusername:mypassword"))
' Build a string with all the params, properly encoded.
' We assume that the arrays paramName and paramVal are
' of equal length:
Dim paramz As New StringBuilder()
For i As Integer = 0 To paramName.Length - 1
paramz.Append(paramName(i))
paramz.Append("=")
paramz.Append(HttpUtility.UrlEncode(paramVal(i)))
paramz.Append("&")
Next
' Encode the parameters as form data:
Dim formData As Byte() = UTF8Encoding.UTF8.GetBytes(paramz.ToString())
req.ContentLength = formData.Length
' Send the request:
Using post As Stream = req.GetRequestStream()
post.Write(formData, 0, formData.Length)
End Using
' Pick up the response:
Dim result As String = Nothing
Using resp As HttpWebResponse = TryCast(req.GetResponse(), HttpWebResponse)
Dim reader As New StreamReader(resp.GetResponseStream())
result = reader.ReadToEnd()
End Using
Return result
End Function
【讨论】:
你已经得到了一些有用的东西,但是你在这里拥有的大部分东西已经在WebClient
中了。以上是关于来自 asp.net/vb 的卷发的主要内容,如果未能解决你的问题,请参考以下文章
ASP.NET - VB.NET - 更新 MS_Access 表
如何在asp.net c# web应用程序中使用asp.net vb页面