我正在尝试从Win7 PC/64bit,Visual Studio 2015,VB.NET发送SMS,使用华为Wingle E8231S -2 GSM WiFi Stick,
GSM WiFi Stick Web界面对我想模仿的API进行呼叫。
要这样做,必须先完成登录请求,而标头包含验证令牌和cookie(会话ID(,我可以通过从Wingle Weberver请求获得。
我总是有响应代码为108006,它是不正确的用户名或密码。
我的代码:
Public Sub Main()
Dim request As WebRequest = WebRequest.Create("http://192.168.8.1/api/webserver/SesTokInfo")
Dim response As WebResponse = request.GetResponse()
Dim dataStream As Stream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Extract the header: cookie is the session id.
Dim SesStart As Short = responseFromServer.IndexOf("<SesInfo>") + 9
Dim SesEnd As Short = responseFromServer.IndexOf("</SesInfo>")
Dim TokStart As Short = responseFromServer.IndexOf("<TokInfo>") + 9
Dim TokEnd As Short = responseFromServer.IndexOf("</TokInfo>")
Dim vSessionId As String = responseFromServer.Substring(SesStart, SesEnd - SesStart)
Dim vToken As String = responseFromServer.Substring(TokStart, TokEnd - TokStart)
' Clean up the streams and the response.
reader.Close()
response.Close()
'Call SetSesAndToken(response)
'Make the api call using the session ID extracted
request = WebRequest.Create("http://192.168.8.1/api/user/login")
request.Headers.Add("__RequestVerificationToken", vToken)
request.Headers.Add("Cookie", vSessionId)
Dim PassWordEncoded As String = Convert.ToBase64String(Encoding.UTF8.GetBytes("admin"))
Dim Myxml As String = "<?xml version:'1.0' encoding='UTF-8'?>
<request>
<Username>admin</Username>
<Password>" & PassWordEncoded & "</Password>
<password_type>4</password_type>
</request>"
Dim bytes As Byte() = Encoding.UTF8.GetBytes(Myxml)
request.Method = "POST"
request.ContentLength = bytes.Length
request.ContentType = "text/xml"
Using requestStream As Stream = request.GetRequestStream()
requestStream.Write(bytes, 0, bytes.Length)
End Using
Dim ReceiveStream As Stream
Dim encode As Encoding
Dim sr As StreamReader
Using myresponse As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
If myresponse.StatusCode <> HttpStatusCode.OK Then
Dim message As String = [String].Format("POST failed. Received HTTP {0}", myresponse.StatusCode)
Throw New ApplicationException(message)
End If
ReceiveStream = myresponse.GetResponseStream()
encode = System.Text.Encoding.GetEncoding("utf-8")
sr = New StreamReader(ReceiveStream)
responseFromServer = sr.ReadToEnd()
MsgBox(responseFromServer)
End Using
您需要拥有一个cookie容器,并保留带有cookie的容器以进行后续调用。您首先要重新验证安全令牌和/或其他cookie,然后拨打其他电话,例如登录等。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Dim FormData As String = "login_user=&wallet=0x29a450d32a95b0b08230b93cc9ac328db81dc80e"
Dim cookiejar As New CookieContainer
Dim webreq As HttpWebRequest = HttpWebRequest.Create("https://somesite.org")
Dim responseReader As StreamReader
Dim responseData As String
Dim requestWriter As StreamWriter
webreq.Method = HttpMethod.Get.Method
webreq.CookieContainer = cookiejar
Dim xxx As HttpWebResponse = webreq.GetResponse()
responseReader = New StreamReader(webreq.GetResponse.GetResponseStream())
responseData = responseReader.ReadToEnd()
responseReader.Close()
webreq = HttpWebRequest.Create("https://somesite.org")
webreq.Accept = "text/html"
webreq.Method = HttpMethod.Post.Method
webreq.ContentType = "application/x-www-form-urlencoded"
webreq.ContentLength = FormData.Length
webreq.CookieContainer = cookiejar
requestWriter = New StreamWriter(webreq.GetRequestStream)
requestWriter.Write(FormData)
requestWriter.Close()
responseReader = New StreamReader(webreq.GetResponse.GetResponseStream())
responseData = responseReader.ReadToEnd()
responseReader.Close()
代码验证您的调制解调器:
Public Sub Main()
' Hitting the starting page
Dim cookiejar As New CookieContainer
Dim request As WebRequest = WebRequest.Create("http://192.168.8.1/api/user/state-login")
request.CookieContainer = cookiejar
Dim response As WebResponse = request.GetResponse()
Dim dataStream As Stream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Extract the header: cookie is the session id.
' Dim SesStart As Short = responseFromServer.IndexOf("<SesInfo>") + 9
' Dim SesEnd As Short = responseFromServer.IndexOf("</SesInfo>")
' Dim TokStart As Short = responseFromServer.IndexOf("<TokInfo>") + 9
' Dim TokEnd As Short = responseFromServer.IndexOf("</TokInfo>")
' Dim vSessionId As String = responseFromServer.Substring(SesStart, SesEnd - SesStart)
' Dim vToken As String = responseFromServer.Substring(TokStart, TokEnd - TokStart)
' Clean up the streams and the response.
reader.Close()
response.Close()
' Hitting the login api endpoint
'Call SetSesAndToken(response)
'Make the api call using the session ID extracted
request = WebRequest.Create("http://192.168.8.1/api/user/login")
request.CookieContainer = cookiejar
Dim PassWordEncoded As String = Convert.ToBase64String(Encoding.UTF8.GetBytes("admin"))
Dim Myxml As String = "<?xml version:'1.0' encoding='UTF-8'?>
<request>
<Username>admin</Username>
<Password>" & PassWordEncoded & "</Password>
<password_type>4</password_type>
</request>"
Dim bytes As Byte() = Encoding.UTF8.GetBytes(Myxml)
request.Method = "POST"
request.ContentLength = bytes.Length
request.ContentType = "text/xml"
Using requestStream As Stream = request.GetRequestStream()
requestStream.Write(bytes, 0, bytes.Length)
End Using
Dim ReceiveStream As Stream
Dim encode As Encoding
Dim sr As StreamReader
Using myresponse As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
If myresponse.StatusCode <> HttpStatusCode.OK Then
Dim message As String = [String].Format("POST failed. Received HTTP {0}", myresponse.StatusCode)
Throw New ApplicationException(message)
End If
ReceiveStream = myresponse.GetResponseStream()
encode = System.Text.Encoding.GetEncoding("utf-8")
sr = New StreamReader(ReceiveStream)
responseFromServer = sr.ReadToEnd()
MsgBox(responseFromServer)
End Using
void SaveUrl(string sourceURL, string savepath) {
CookieContainer cookies = new CookieContainer();
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(sourceURL);
webRequest.CookieContainer = cookies;
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
StreamReader responseReader = new StreamReader(response.GetResponseStream());
string sResponseHTML = responseReader.ReadToEnd();
using (StreamWriter sw = new StreamWriter(savepath, false)) {
sw.Write(sResponseHTML);
}
string[] ImageUrl = GetImgLinks(sResponseHTML);
foreach (string imagelink in ImageUrl) {
HttpWebRequest imgRequest = (HttpWebRequest)WebRequest.Create(imagelink);
imgRequest.CookieContainer = cookies;
HttpWebResponse imgresponse = (HttpWebResponse)imgRequest.GetResponse();
//Code to save image
}
}
测试它,我不是一个.net的家伙。祝你好运!
清洁程序的方法是为HTTP请求创建会话,而不是点击单个API端点。在这种情况下,cookie将默认发送。如果您有足够的时间可以在命令上探索。祝你好运!
代码验证您的调制解调器:
Public Sub Main()
' Hitting the starting page
Dim cookiejar As New CookieContainer
Dim request As WebRequest = WebRequest.Create("http://192.168.8.1/api/user/state-login")
request.CookieContainer = cookiejar
Dim response As WebResponse = request.GetResponse()
Dim dataStream As Stream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Extract the header: cookie is the session id.
' Dim SesStart As Short = responseFromServer.IndexOf("<SesInfo>") + 9
' Dim SesEnd As Short = responseFromServer.IndexOf("</SesInfo>")
' Dim TokStart As Short = responseFromServer.IndexOf("<TokInfo>") + 9
' Dim TokEnd As Short = responseFromServer.IndexOf("</TokInfo>")
' Dim vSessionId As String = responseFromServer.Substring(SesStart, SesEnd - SesStart)
' Dim vToken As String = responseFromServer.Substring(TokStart, TokEnd - TokStart)
' Clean up the streams and the response.
reader.Close()
response.Close()
' Hitting the login api endpoint
'Call SetSesAndToken(response)
'Make the api call using the session ID extracted
request = WebRequest.Create("http://192.168.8.1/api/user/login")
request.CookieContainer = cookiejar
Dim PassWordEncoded As String = Convert.ToBase64String(Encoding.UTF8.GetBytes("admin"))
Dim Myxml As String = "<?xml version:'1.0' encoding='UTF-8'?>
<request>
<Username>admin</Username>
<Password>" & PassWordEncoded & "</Password>
<password_type>4</password_type>
</request>"
Dim bytes As Byte() = Encoding.UTF8.GetBytes(Myxml)
request.Method = "POST"
request.ContentLength = bytes.Length
request.ContentType = "text/xml"
Using requestStream As Stream = request.GetRequestStream()
requestStream.Write(bytes, 0, bytes.Length)
End Using
Dim ReceiveStream As Stream
Dim encode As Encoding
Dim sr As StreamReader
Using myresponse As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
If myresponse.StatusCode <> HttpStatusCode.OK Then
Dim message As String = [String].Format("POST failed. Received HTTP {0}", myresponse.StatusCode)
Throw New ApplicationException(message)
End If
ReceiveStream = myresponse.GetResponseStream()
encode = System.Text.Encoding.GetEncoding("utf-8")
sr = New StreamReader(ReceiveStream)
responseFromServer = sr.ReadToEnd()
MsgBox(responseFromServer)
End Using
void SaveUrl(string sourceURL, string savepath) {
CookieContainer cookies = new CookieContainer();
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(sourceURL);
webRequest.CookieContainer = cookies;
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
StreamReader responseReader = new StreamReader(response.GetResponseStream());
string sResponseHTML = responseReader.ReadToEnd();
using (StreamWriter sw = new StreamWriter(savepath, false)) {
sw.Write(sResponseHTML);
}
string[] ImageUrl = GetImgLinks(sResponseHTML);
foreach (string imagelink in ImageUrl) {
HttpWebRequest imgRequest = (HttpWebRequest)WebRequest.Create(imagelink);
imgRequest.CookieContainer = cookies;
HttpWebResponse imgresponse = (HttpWebResponse)imgRequest.GetResponse();
//Code to save image
}
}
请测试我不是.NET家伙。祝你好运!