2016-05-13 70 views
1

我試圖利用使用GET方法的REST服務。我正在使用.Net Framework 4.5.2。我在下面寫的只是一個測試,看看我是否可以實際提出請求。如果我把URL直接放到瀏覽器中,我會以json格式的字符串數據得到很好的迴應。但是當我嘗試運行代碼時,我最終得到了以下錯誤:GetReponse()上的「底層連接已關閉:發送時發生意外錯誤」但不通過瀏覽器

「底層連接已關閉:發送時出現意外錯誤。」 InnerException = {「無法從傳輸連接讀取數據:現有連接被遠程主機強制關閉。」}

我試過設置keepalive爲true和false,我試過使用WebClient以及DownloadString太....都導致相同的錯誤。有沒有人知道我錯過了什麼?或者這是服務器端的某種問題?

Dim script As String = "236" 
Dim deploy As String = "1" 
Dim compid As String = "915960_SB2" 
Dim h As String = "value1" 
Dim id As String = "1241389" 
Dim status As String = "in freezer" 
Dim request As HttpWebRequest 
Dim response As HttpWebResponse 

Try 

'I have removed the real website name from this code. 
      Dim theurl As String = "https://website/app/site/hosting/scriptlet.nl?script={0}&deploy={1}&compid={2}&h={3}&Id={4}&Status={5}" 
      Dim url As String = String.Format(theurl, script, deploy, compid, h, id, status) 

      request = HttpWebRequest.Create(url) 
      request.Method = "GET" 
      request.ContentType = "application/json" 

'This is where the error occurs 
      response = request.GetResponse() 

Catch ex As Exception 
      Dim test as string="" 
Finally 

End Try 
+0

我推薦使用的工具像Fiddler檢查HTTP流量,看看是否有您的瀏覽器請求和你的代碼之間的差異。一個小問題:'Content-Type:application/json'對GET請求沒有任何意義。你可能會想'Accept:application/json'。 –

+0

嗯,我解決了它..我偶然發現了其他人建議嘗試設置安全協議。所以在我的代碼的頂部,並寫下這一行:ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12,它的工作。 –

回答

相關問題