2017-01-12 64 views
0

我有一個簡單的web服務器運行在一個wifi芯片(esp8266,寫在LUA上,運行在nodeMCU上)。當我在任何瀏覽器中查詢網站時,我收到了預期的響應並顯示在瀏覽器中(約45個字符長)。當我在C#或PowerShell中進行查詢,我得到:底層連接已關閉;但在瀏覽器中工作

「的基礎連接已關閉:連接被意外關閉 。」

我已經嘗試了許多選項,建議跨越許多論壇,但他們都沒有工作。

有什麼辦法可以像IE或Chrome一樣使用Web請求?我不確定瀏覽器在內部採取了哪些額外步驟,以便他們能夠毫無問題地獲得響應?爲什麼在.NET中這是一個問題?

我的腳本如下。在絕望之中,我正在考慮使用c#來關閉PhantomJS(無頭瀏覽器),然後使用javascript來告訴它打開網站,然後傳回響應。或者,也可以使用套接字打開連接並以此方式進行,而不是依賴.NET封裝。

# Set the useUnsafeHeaderParsing property to true 
$netAssembly = [Reflection.Assembly]::GetAssembly([System.Net.Configuration.SettingsSection]) 
$bindingFlags = [Reflection.BindingFlags] "Static,GetProperty,NonPublic" 
$settingsType = $netAssembly.GetType("System.Net.Configuration.SettingsSectionInternal") 
$instance = $settingsType.InvokeMember("Section", $bindingFlags, $null, $null, @()) 
if($instance) 
{ 
    $bindingFlags = "NonPublic","Instance" 
    $useUnsafeHeaderParsingField = $settingsType.GetField("useUnsafeHeaderParsing", $bindingFlags) 

    if($useUnsafeHeaderParsingField) 
    { 
     $useUnsafeHeaderParsingField.SetValue($instance, $true) 
    } 
} 

# Try setting the certificate policy to a custom child class that always returns true 
add-type @" 
    using System.Net; 
    using System.Security.Cryptography.X509Certificates; 
    public class TrustAllCertsPolicy : ICertificatePolicy { 
     public bool CheckValidationResult(
      ServicePoint srvPoint, X509Certificate certificate, 
      WebRequest request, int certificateProblem) { 
      return true; 
     } 
    } 
"@ 
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy 

# Try setting other attributes on the ServicePointManager class 
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls -bxor [System.Net.SecurityProtocolType]::Ssl3 
[System.Net.ServicePointManager]::Expect100Continue = $false; 

# Initiate the web request 
$r = [System.Net.WebRequest]::Create("http://192.168.1.7/GetStatusAsJson") 
# Try long timeouts, with KeepAlive set to false; Also try giving it a user agent string etc. 
$r.Timeout = 5000 
$r.ReadWriteTimeout = 5000 
$r.KeepAlive = $false 
$r.Method = "GET" 
$r.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36"; 
$r.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; 
$resp = $r.GetResponse() 
+0

什麼是所有'$'s? –

+1

「我不確定瀏覽器做了什麼額外的步驟」......提琴手可能會幫助你。 – spender

+0

如果嘗試使用不同的安全協議,會發生什麼情況:[Net.ServicePointManager] :: SecurityProtocol = [Net.SecurityProtocolType] :: Tls12' – BenH

回答

0

我最終使用MSXML2.XMLHTTP.3.0與vbscript,並從powershell執行vbscript。

on error resume next 
Dim oXMLHTTP 
Dim oStream 

Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.3.0") 
oXMLHTTP.Open "GET", WScript.Arguments.Item(2) & "/OpenDoor", False 
oXMLHTTP.Send 

If oXMLHTTP.Status = 200 Then 
    Set oStream = CreateObject("ADODB.Stream") 
    oStream.Open 
    oStream.Type = 1 
    oStream.Write oXMLHTTP.responseBody 
    oStream.SaveToFile WScript.Arguments.Item(0) & "\OpenDoor.html" 
    oStream.Close 
End If 
相關問題