WebClient 抛出 SocketException(无效参数)
WebClient throws SocketException (Invalid argument)
我使用下面的代码尝试创建一个文件到 ftp 服务器。但是我得到一个套接字异常。谁能帮我解决这个异常?当我使用 UploadFile(uri, filepath)
函数时似乎会发生同样的问题。
我也尝试在 WebClient 的选项中将凭据设置为 NetworkCredentials
,但得到了相同的结果。
在远程 ftp 服务器上也得到了相同的结果。
我正在使用 .NET 4.7.1
byte[] xmlBytes = XmlDocumentToByteArray(xml);
WebClient webclient = new WebClient() {
Proxy = null
};
Uri uri = new Uri("ftp://test:test@localhost/test.xml");
//Uri uri = new Uri("ftp://localhost/test.xml");
webclient.UploadData(uri, xmlBytes);
异常:
(英文:提供了无效参数)
System.Net.WebException: Er is een ongeldig argument opgegeven ---> System.Net.Sockets.SocketException: Er is een ongeldig argument opgegeven
bij System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
bij System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
bij System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
bij System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
bij System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
bij System.Net.FtpWebRequest.QueueOrCreateConnection()
bij System.Net.FtpWebRequest.SubmitRequest(Boolean async)
--- Einde van intern uitzonderingsstackpad ---
bij System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request)
bij System.Net.WebClient.UploadData(Uri address, String method, Byte[] data)
bij System.Net.WebClient.UploadData(Uri address, Byte[] data)
bij ProbeOrderPlacement.Order.OrderSubmit.SubmitOrder(XmlDocument xml) in H:\VSWorkspace\VSTS\Quantore\Probe\ProbeOrderPlacement\Order\OrderPlacement.cs:regel 28
也尝试了以下代码,但它抛出相同的异常(但在 .GetRequestStream()
上)
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://127.0.0.1/test.xml"));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Proxy = null;
request.UseBinary = true;
request.Credentials = new NetworkCredential("test", "test");
request.ContentLength = xmlBytes.Length;
//Upload the data
using(Stream requestStream = request.GetRequestStream()) {
requestStream.Write(xmlBytes, 0, xmlBytes.Length);
}
异常:
(英文:提供了无效参数)
System.Net.WebException: Er is een ongeldig argument opgegeven ---> System.Net.Sockets.SocketException: Er is een ongeldig argument opgegeven
bij System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
bij System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
bij System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
bij System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
bij System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
bij System.Net.FtpWebRequest.QueueOrCreateConnection()
bij System.Net.FtpWebRequest.SubmitRequest(Boolean async)
--- Einde van intern uitzonderingsstackpad ---
bij System.Net.FtpWebRequest.GetRequestStream()
bij ProbeOrderPlacement.Order.OrderSubmit.SubmitOrder(XmlDocument xml)
查看 WebClient.UploadFile Method。第二个参数是文件 name.There 对于 byte[]
没有重载
试试这个
webclient.UploadData(uri, xmlfilename);
你的ftpurl好像有误。将您的代码更改为
byte[] xmlBytes = XmlDocumentToByteArray(xml);
WebClient webclient = new WebClient
{
Proxy = null,
Credentials = new NetworkCredential("test", "test")
};
Uri uri = new Uri("ftp://localhost/test.xml");
webclient.UploadData(uri, xmlBytes);
除了一处更改(读取 XML 文件并将其转换为 byte[]
)之外,我只是按原样尝试了您的代码,我能够毫无问题地上传文件。
由于您没有分享您的 XML 结构或您用来读取 XML 并将其转换为 byte[]
的方法,我使用了 最简单的形式XML和.NET
中的开箱即用的方法读取文件数据并将其转换为byte[]
。
这是来自 source.xml 文件的 XML 文本:
<System>
<E01></E01>
<E02></E02>
<E03></E03>
</System>
这是我在 ftp 服务器上读取转换上传 source.xml 的代码:
byte[] xmlBytes = File.ReadAllBytes ("D:\source.xml");
WebClient webclient = new WebClient ()
{
Proxy = null
};
Uri uri = new Uri ("ftp://ftp_user:ftp_password@localhost/test.xml");
//Uri uri = new Uri("ftp://localhost/test.xml");
webclient.UploadData (uri, xmlBytes);
NOTE: This is a Winforms application build on .NET Framework 4.7.1
如果您对XML结构或阅读文件的方式有任何具体要求,您可以分享相同的内容,我们可以进一步讨论。
或者,您可以在 Github 上分享您的解决方案的精简版本,以便其他人进行故障排除。
希望对您有所帮助!
基于 Hans Passant 的评论
This is a very low-level mishap, nothing to do with the arguments you passed since they don't get used until later. The underlying OS function (WSASocket) failed, seemingly unhappy about creating a TCP socket. That should never happen of course. But crap happens with networking code, there is always far too much junk hanging off a socket that professes to keep your machine safe and running smoothly. Disable the installed anti-malware product first, any kind of firewall and whatnot next.
我发现对于我的情况,问题在于我在公司提供给我的网络驱动器上有我的解决方案存储库。我最好的猜测是上传失败是由于潜在的安全措施,尽管我没有深入研究它以能够证明是这种情况。
在我的 C 盘上创建一个新的本地存储库解决了这个问题。
因此,对于遇到相同问题的功能读者的建议是查看底层基础架构以解决问题。
我使用下面的代码尝试创建一个文件到 ftp 服务器。但是我得到一个套接字异常。谁能帮我解决这个异常?当我使用 UploadFile(uri, filepath)
函数时似乎会发生同样的问题。
我也尝试在 WebClient 的选项中将凭据设置为 NetworkCredentials
,但得到了相同的结果。
在远程 ftp 服务器上也得到了相同的结果。
我正在使用 .NET 4.7.1
byte[] xmlBytes = XmlDocumentToByteArray(xml);
WebClient webclient = new WebClient() {
Proxy = null
};
Uri uri = new Uri("ftp://test:test@localhost/test.xml");
//Uri uri = new Uri("ftp://localhost/test.xml");
webclient.UploadData(uri, xmlBytes);
异常: (英文:提供了无效参数)
System.Net.WebException: Er is een ongeldig argument opgegeven ---> System.Net.Sockets.SocketException: Er is een ongeldig argument opgegeven
bij System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
bij System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
bij System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
bij System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
bij System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
bij System.Net.FtpWebRequest.QueueOrCreateConnection()
bij System.Net.FtpWebRequest.SubmitRequest(Boolean async)
--- Einde van intern uitzonderingsstackpad ---
bij System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request)
bij System.Net.WebClient.UploadData(Uri address, String method, Byte[] data)
bij System.Net.WebClient.UploadData(Uri address, Byte[] data)
bij ProbeOrderPlacement.Order.OrderSubmit.SubmitOrder(XmlDocument xml) in H:\VSWorkspace\VSTS\Quantore\Probe\ProbeOrderPlacement\Order\OrderPlacement.cs:regel 28
也尝试了以下代码,但它抛出相同的异常(但在 .GetRequestStream()
上)
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://127.0.0.1/test.xml"));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Proxy = null;
request.UseBinary = true;
request.Credentials = new NetworkCredential("test", "test");
request.ContentLength = xmlBytes.Length;
//Upload the data
using(Stream requestStream = request.GetRequestStream()) {
requestStream.Write(xmlBytes, 0, xmlBytes.Length);
}
异常: (英文:提供了无效参数)
System.Net.WebException: Er is een ongeldig argument opgegeven ---> System.Net.Sockets.SocketException: Er is een ongeldig argument opgegeven
bij System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
bij System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
bij System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
bij System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
bij System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
bij System.Net.FtpWebRequest.QueueOrCreateConnection()
bij System.Net.FtpWebRequest.SubmitRequest(Boolean async)
--- Einde van intern uitzonderingsstackpad ---
bij System.Net.FtpWebRequest.GetRequestStream()
bij ProbeOrderPlacement.Order.OrderSubmit.SubmitOrder(XmlDocument xml)
查看 WebClient.UploadFile Method。第二个参数是文件 name.There 对于 byte[]
试试这个
webclient.UploadData(uri, xmlfilename);
你的ftpurl好像有误。将您的代码更改为
byte[] xmlBytes = XmlDocumentToByteArray(xml);
WebClient webclient = new WebClient
{
Proxy = null,
Credentials = new NetworkCredential("test", "test")
};
Uri uri = new Uri("ftp://localhost/test.xml");
webclient.UploadData(uri, xmlBytes);
除了一处更改(读取 XML 文件并将其转换为 byte[]
)之外,我只是按原样尝试了您的代码,我能够毫无问题地上传文件。
由于您没有分享您的 XML 结构或您用来读取 XML 并将其转换为 byte[]
的方法,我使用了 最简单的形式XML和.NET
中的开箱即用的方法读取文件数据并将其转换为byte[]
。
这是来自 source.xml 文件的 XML 文本:
<System>
<E01></E01>
<E02></E02>
<E03></E03>
</System>
这是我在 ftp 服务器上读取转换上传 source.xml 的代码:
byte[] xmlBytes = File.ReadAllBytes ("D:\source.xml");
WebClient webclient = new WebClient ()
{
Proxy = null
};
Uri uri = new Uri ("ftp://ftp_user:ftp_password@localhost/test.xml");
//Uri uri = new Uri("ftp://localhost/test.xml");
webclient.UploadData (uri, xmlBytes);
NOTE: This is a Winforms application build on .NET Framework 4.7.1
如果您对XML结构或阅读文件的方式有任何具体要求,您可以分享相同的内容,我们可以进一步讨论。
或者,您可以在 Github 上分享您的解决方案的精简版本,以便其他人进行故障排除。
希望对您有所帮助!
基于 Hans Passant 的评论
This is a very low-level mishap, nothing to do with the arguments you passed since they don't get used until later. The underlying OS function (WSASocket) failed, seemingly unhappy about creating a TCP socket. That should never happen of course. But crap happens with networking code, there is always far too much junk hanging off a socket that professes to keep your machine safe and running smoothly. Disable the installed anti-malware product first, any kind of firewall and whatnot next.
我发现对于我的情况,问题在于我在公司提供给我的网络驱动器上有我的解决方案存储库。我最好的猜测是上传失败是由于潜在的安全措施,尽管我没有深入研究它以能够证明是这种情况。
在我的 C 盘上创建一个新的本地存储库解决了这个问题。
因此,对于遇到相同问题的功能读者的建议是查看底层基础架构以解决问题。