Web客户端无法连接到远程服务器

时间:2014-03-09 11:32:01

标签: vb.net

我正在尝试使用似乎位于代理服务器后面的PC在网站上阅读文本文件。最初我有407个错误,但后来我补充说;

<system.net>
  <defaultProxy useDefaultCredentials="true">
    <proxy usesystemdefault="True" />
  </defaultProxy>
</system.net>

到app.config,现在我得到“无法连接到远程服务器”。

我的代码是:

' Make a WebClient.
myWebClient = New System.Net.WebClient

' Get the indicated URI.
Dim response As Stream = myWebClient.OpenRead("mySite/myfile.txt")

' Read the result.
Dim objreader As New IO.StreamReader(response)

如果我从同一台PC将“mySite / myfile.txt”输入浏览器,则网站上会显示该文件。

我是否有代码错误,或者我的应用可以解决这个问题吗?

我已经通过恢复到VB5版本解决了这个问题,该版本使用API​​调用来下载文本文件。 谢谢Randy Birch。

'//API to download file 
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
   (ByVal pCaller As Integer, ByVal szURL As String, ByVal szFileName As String,  ByVal  dwReserved As Integer, ByVal lpfnCB As Integer) As Integer

Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" Alias     "DeleteUrlCacheEntryA" (ByVal lpszUrlName As String) As Integer

Private Const ERROR_SUCCESS As Integer = 0
Private Const BINDF_GETNEWESTVERSION As Integer = &H10
'//

Private Function DownloadFileAPI(ByRef sSourceUrl As String, ByRef sLocalFile As String) As Boolean

    'http://vbnet.mvps.org/index.html?code/internet/urldownloadtofile.htm
    'http://vbnet.mvps.org/index.html?code/internet/urldownloadtofilenocache.htm

    Call DeleteUrlCacheEntry(sSourceUrl)

    'Download the file. BINDF_GETNEWESTVERSION forces
    'the API to download from the specified source.
    'Passing 0& as dwReserved causes the locally-cached
    'copy to be downloaded, if available. If the API
    'returns ERROR_SUCCESS (0), DownloadFile returns True.
    DownloadFileAPI = URLDownloadToFile(0, sSourceUrl, sLocalFile, BINDF_GETNEWESTVERSION, 0) = ERROR_SUCCESS
End Function

并在代码中;

    Dim retbol As Boolean
    Dim URL As String = "mySite/myFiletodownload"

    fsavename = "mysavefile" 'path\filename to save downloaded file to.

    Try
        'This should return true or false so might not trigger an exception
        retbol = DownloadFileAPI(URL, fsavename)
  'etc

0 个答案:

没有答案