Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

vb.net - How can I pass on the access data (Credentail) to the HttpClient?

I am trying to take a picture with my camera and show it on the web. I've done everything that has to be done. But it still doesn't work. I have already done the post, takePicture and SendRequest functions, but the code where I give the credentails to the httpclient is missing, which is exactly where I have no idea.

Public Class theta
Public THETA_ID As String = "name"
Public THETA_PASSWORD As String = "password"
Public THETA_IP As String = "http://11.11.11.11/"
Public THETA_URL As String = THETA_IP & "osc/"
Public response As String

Function post(osc_command As String) As String
    Dim url = THETA_URL + osc_command
    Dim request As Net.WebRequest = Net.WebRequest.Create(url)
    request.Credentials = New Net.NetworkCredential(THETA_ID, THETA_PASSWORD)
    Dim resp As Net.WebResponse = request.GetResponse()
End Function

Public Async Function takePicture() As Task(Of String)
    Dim url = THETA_URL & "commands/execute"
    Dim payload = New Dictionary(Of Object, Object) From {
            {"name", "camera.takePicture"}
        }
    ServicePointManager.Expect100Continue = False
    Dim request As Net.HttpWebRequest = Net.HttpWebRequest.Create(url)
    request.Credentials = New Net.NetworkCredential(THETA_ID, THETA_PASSWORD)
    Dim x As New Http.HttpClient
    Dim content As New Net.Http.StringContent("name:camera.takepicture", System.Text.Encoding.UTF8, "application/json")
    Dim response As Net.Http.HttpResponseMessage = Await x.PostAsync(url, content)
    Dim result As String = Await response.Content.ReadAsStringAsync()
    response = response
End Function


Function SendRequest(actionstring As String) As XDocument
    Dim url = THETA_URL & "commands/execute"
    Dim postXml As XDocument = <?xml version='1.0'?>
                               <txtsig_request version='2.0'>
                                   <credentials>
                                       <api_username><%= THETA_ID %></api_username>
                                       <api_password><%= THETA_PASSWORD %></api_password>
                                       <client_id><%= "XX" %></client_id>
                                   </credentials>
                                   <%= XElement.Parse(actionstring) %>
                               </txtsig_request>

    Dim postString As String = postXml.Declaration.ToString & postXml.ToString

    Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
    request.Method = "POST"
    request.AllowWriteStreamBuffering = False
    request.PreAuthenticate = True
    request.ContentType = "application/xml"
    request.ContentLength = postString.Length
    request.KeepAlive = True

    Dim outputstream As Stream = request.GetRequestStream()
    Dim postBytes As Byte() = Encoding.ASCII.GetBytes(postString)
    outputstream.Write(postBytes, 0, postBytes.Length)

    Dim response As WebResponse = request.GetResponse()
    Dim datastream As Stream = response.GetResponseStream()
    Dim xd As XDocument = XDocument.Load(datastream, LoadOptions.None)

    outputstream.Close()
    response.Close()

    Return xd
End Function

Sub New()
    'post("state")
    'takePicture()
End Sub

End Class

question from:https://stackoverflow.com/questions/65899371/how-can-i-pass-on-the-access-data-credentail-to-the-httpclient

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...