本文介绍了通过对象传递networkcredentails时的对象引用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! hi i我正在使用FTP下载文件。 所以即时使用FtpWebRequest类。 当我通过网络凭证硬编码时,我没有收到任何错误。 但是当我使用NetworkCredential对象传递凭证时,我得到一个错误'对象引用没有设置为对象的实例'。 以下是您的评论代码: ' 这种方式不会产生错误 Dim lo As 新 clsFTPDownload(新 NetworkCredential( userid, userpwd),FTP_Path) ' dim ftpcred as new NetworkCredential ' ftpcred.UserName =userid ' ftpcred.Password =userpwd ' dim lo As new clsFTPDownload(ftpcred,FTP_Path)'产生错误 Dim imgdata 作为 字节()= lo.Download 公开 class clsFTPDownload private _ftpcredentails as new NetworkCredential private _ftppath as uri private _IsKeepAlive as boolean sub new ( byval ftpcred as NetworkCredential, byval fpath as uri) _IsKeepAlive = false _ftpcredentails = ftpcred _ftppath = fpath end sub 公共 功能下载() 作为 字节() Dim reqFTP 作为 FtpWebRequest 尝试 如果 _ftppath.ToString.Trim = 字符串 .Empty 那么 抛出 新异常( 空白FTP路径) 结束 如果 reqFTP = CType (FtpWebRequest.Create(_ftppath),FtpWebRequest) reqFTP.Method = WebRequestMethods.Ftp.DownloadFile reqFTP.Credentials = _ftpcredentails reqFTP.KeepAlive = _IsKeepAlive reqFTP.UseBinary = True reqFTP.Proxy = 没什么 reqFTP.UsePassive = 错误 昏暗 response As WebResponse = CType (reqFTP.GetResponse,FtpWebResponse)' 此处遇到对象引用错误 Dim responseStream As Stream = response.GetResponseStream Dim mem as 新 MemoryStream responseStream.CopyTo(mem) Dim bytes As Byte ()= mem.GetBuffer mem.Close() response.Close() 返回字节 Catch wEx As WebException throw Catch ex As Exceptio n 投掷 最后 reqFTP.Abort() 结束 尝试 结束 功能 end class 解决方案 http://mattmitchell.com.au/zebras-in-system-net-networkcredential/ 参考上面的链接: dim ftpcred as new NetworkCredential ftpcred。 UserName = userid ftpcred.Password = userpwd ftpcred.domain = ' 明确添加 dim lo 作为 新 clsFTPDownload(ftpcred,FTP_Path)' 现在不会产生错误 hii am using FTP for downloading a file.so i m using FtpWebRequest class for the same.when i m passing the network credentials hardcoded, i don't receive any error.but when i m using a NetworkCredential object to pass the credentails , i get an error 'object reference not set to an instance of object'.Below is the code for your review:'this way doesn't produce errorDim lo As New clsFTPDownload(New NetworkCredential("userid", "userpwd"), FTP_Path)'dim ftpcred as new NetworkCredential'ftpcred.UserName="userid"'ftpcred.Password="userpwd"'dim lo As New clsFTPDownload(ftpcred, FTP_Path) 'produces errorDim imgdata As Byte() = lo.DownloadPublic class clsFTPDownloadprivate _ftpcredentails as new NetworkCredentialprivate _ftppath as uriprivate _IsKeepAlive as booleansub new(byval ftpcred as NetworkCredential, byval fpath as uri) _IsKeepAlive=false _ftpcredentails =ftpcred _ftppath =fpathend subPublic Function Download() As Byte() Dim reqFTP As FtpWebRequest Try If _ftppath.ToString.Trim = String.Empty Then Throw New Exception("blank FTP path") End If reqFTP = CType(FtpWebRequest.Create(_ftppath), FtpWebRequest) reqFTP.Method = WebRequestMethods.Ftp.DownloadFile reqFTP.Credentials = _ftpcredentails reqFTP.KeepAlive = _IsKeepAlive reqFTP.UseBinary = True reqFTP.Proxy = Nothing reqFTP.UsePassive = False Dim response As WebResponse = CType(reqFTP.GetResponse, FtpWebResponse) 'here object reference error is encountered Dim responseStream As Stream = response.GetResponseStream Dim mem As New MemoryStream responseStream.CopyTo(mem) Dim bytes As Byte() = mem.GetBuffer mem.Close() response.Close() Return bytes Catch wEx As WebException throw Catch ex As Exception Throw Finally reqFTP.Abort() End Try End Functionend class 解决方案 http://mattmitchell.com.au/zebras-in-system-net-networkcredential/referring to above link:dim ftpcred as new NetworkCredentialftpcred.UserName="userid"ftpcred.Password="userpwd"ftpcred.domain="" 'added explicitlydim lo As New clsFTPDownload(ftpcred, FTP_Path) 'won't produce error now 这篇关于通过对象传递networkcredentails时的对象引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-19 09:53