我有一些vb.net代码,我正尝试使用它在donmains.google.com上设置新域的ip地址
代码如下:
Dim user As String = "MYRESOURSEID"
Dim pwd As String = "MYRESOURCEPASSWORD"
Dim host As String = "MYDOMAIN"
Dim address As String = "https://" & user & ":" & pwd & "@domains.google.com/nic/update?hostname=" & host
Dim request As HttpWebRequest = WebRequest.Create(address)
request.Method = "POST"
request.Host = "domains.google.com"
request.UserAgent = "Chrome/41.0"
request.Headers.Add("From:mkhiliger@gmail.com")
request.Headers.Add("Authorization:Basic base64-encoded-auth-string")
request.ContentLength = 0
Dim response As HttpWebResponse = request.GetResponse()
Dim dataStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
reader.Close()
response.Close()如果我将address变量的值粘贴到浏览器中,它会很好地设置域,但当我使用此代码时,来自服务器的响应是错误的身份验证。
有人知道这可能出了什么问题吗?
发布于 2018-05-18 22:51:19
你的凭证没有编码--规范中提到“注意:谷歌域名使用dyndns2协议。”所以我认为你可以重新编写这个DynDNS子模块:
Private Sub DynUpdate(HostAddress As String, UserName As String, PassWord As String, MyWANIP As String, DynDNS_Agent As String)
Dim MySocket As New TcpClient
Dim MyStream As NetworkStream
Dim MyUpdateURL() As Byte
Dim MyReplyHTML As String
Dim BytesRead As Integer
Dim MyBuffer(4096) As Byte
Dim Offset As Integer
Dim ReturnCode, MoreInfo As String
'Dim LastUpdate As Date
Dim DynResultText As New Collections.Specialized.StringDictionary
DynResultText.Add("badsys", "The system parameter given is not valid. Valid system parameters are dyndns, statdns and custom")
DynResultText.Add("badagent", "The user agent that was sent has been blocked for not following specifications or no user agent was specified")
DynResultText.Add("badauth", "The username or password specified are incorrect")
DynResultText.Add("!donator", "An option available only to credited users (such as offline URL) was specified, but the user is not a credited user")
Try
' Init and open the socket.
MySocket.ReceiveTimeout = 20000
MySocket.Connect("members.dyndns.org", 80)
MyStream = MySocket.GetStream()
' Write the request.
MyUpdateURL = ASCII.GetBytes("GET /nic/update?hostname=" & HostAddress & "&myip=" & MyWANIP & " HTTP/1.0" & vbCrLf & "Host: members.dyndns.org" & vbCrLf & "Authorization: Basic " & System.Convert.ToBase64String(ASCII.GetBytes(UserName & ":" & PassWord)) & vbCrLf & "User-Agent: " & DynDNS_Agent & vbCrLf & vbCrLf)
MyStream.Write(MyUpdateURL, 0, MyUpdateURL.Length)
' Read the respons.
BytesRead = MyStream.Read(MyBuffer, 0, 4096)
If BytesRead > 0 Then
MyReplyHTML = ASCII.GetString(MyBuffer, 0, BytesRead)
Offset = InStr(MyReplyHTML, vbCrLf & vbCrLf)
If Offset > 0 And MyReplyHTML.Length > Offset + 4 Then
ReturnCode = Split(Mid(MyReplyHTML, Offset + 4), " ")(0)
Select Case LCase(ReturnCode)
Case "badsys", "badagent", "badauth", "notfqdn", "nohost", "numhost", "abuse"
' "Server Update failed - Action Required")
If DynResultText.ContainsKey(ReturnCode) Then MoreInfo = DynResultText.Item(ReturnCode)
'should disable further updates
Case "good" ' The update was successful, and the hostname is now updated.
Case "nochg" ' The update was unnecessary, and the hostname is unchanged.
Case "dnserr", "911"
' SeriousError
Case Else
' "Server Update failed - Return code '" & ReturnCode & "'")
End Select
End If
End If
' Clean up.
MyStream = Nothing
MySocket.Close()
MySocket = Nothing
Catch ex As Exception
' "Error Updating")
End Try
End Subhttps://stackoverflow.com/questions/44252329
复制相似问题