我在本地做一个网站,其中要向外网的的ftp服务器上传文件,在局域网内没有问题,向外网传输是报:
System.Net.WebException: 基础连接已经关闭: 服务器提交了协议冲突。
在 System.Net.FtpWebRequest.CheckError()
在 System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
在 System.Net.FtpWebRequest.RequestCallback(Object obj)
在 System.Net.CommandStream.InvokeRequestCallback(Object obj)
在 System.Net.CommandStream.Abort(Exception e)
在 System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
在 System.Net.FtpWebRequest.GetResponse()
在 _Default.Upload(String filename) 位置 e:\workeWorke\FtpShangChuan\Default.aspx.cs:行号 53
我的代码是: public void Upload(string filename) //上面的代码实现了从ftp服务器上载文件的功能
{ FileInfo fileInf = new FileInfo(filename); string uri = "ftp://192.168.1.116/Aba_Zixun_Kejiqianyan_2.html"
Connect(uri);//连接
// 默认为true,连接不会被关闭
// 在一个命令之后被执行 reqFTP.KeepAlive = true;
// 指定执行什么命令 reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 上传文件时通知服务器文件的大小 reqFTP.ContentLength = fileInf.Length;
// 缓冲大小设置为kb
reqFTP.EnableSsl = true;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// 打开一个文件流(System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
try
{
WebResponse web=reqFTP.GetResponse();
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream();
// 每次读文件流的kb
contentLen = fs.Read(buff, 0, buffLength);
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// 关闭两个流
strm.Close();
fs.Close();
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
} }
//连接ftp
private void Connect(String path)
{
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
// 指定数据传输类型
reqFTP.UseBinary = true;
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential("user", "62500"); }
谢谢
System.Net.WebException: 基础连接已经关闭: 服务器提交了协议冲突。
在 System.Net.FtpWebRequest.CheckError()
在 System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
在 System.Net.FtpWebRequest.RequestCallback(Object obj)
在 System.Net.CommandStream.InvokeRequestCallback(Object obj)
在 System.Net.CommandStream.Abort(Exception e)
在 System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
在 System.Net.FtpWebRequest.GetResponse()
在 _Default.Upload(String filename) 位置 e:\workeWorke\FtpShangChuan\Default.aspx.cs:行号 53
我的代码是: public void Upload(string filename) //上面的代码实现了从ftp服务器上载文件的功能
{ FileInfo fileInf = new FileInfo(filename); string uri = "ftp://192.168.1.116/Aba_Zixun_Kejiqianyan_2.html"
Connect(uri);//连接
// 默认为true,连接不会被关闭
// 在一个命令之后被执行 reqFTP.KeepAlive = true;
// 指定执行什么命令 reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 上传文件时通知服务器文件的大小 reqFTP.ContentLength = fileInf.Length;
// 缓冲大小设置为kb
reqFTP.EnableSsl = true;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// 打开一个文件流(System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
try
{
WebResponse web=reqFTP.GetResponse();
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream();
// 每次读文件流的kb
contentLen = fs.Read(buff, 0, buffLength);
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// 关闭两个流
strm.Close();
fs.Close();
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
} }
//连接ftp
private void Connect(String path)
{
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
// 指定数据传输类型
reqFTP.UseBinary = true;
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential("user", "62500"); }
谢谢
解决方案 »
- 「玩一玩」自制虚拟机,第一部分:设计和汇编器
- treeview中修改修改的节点的数据怎么存到数据库
- C#中 可以给这样赋值吗?Object O1= "Something"
- 两类数据A和B,A中的元素唯一确定一个B类元素,B类的一个元素唯一
- 问个关于datatable.GetChanges()的问题,高手来看下
- SQL问题
- c#textbox实现换页功能
- 想使用ClickOnce,但不想让用户安装程序!
- 请教一个正则表达式。。。。。。。。。。正则表达式
- SqlConnection Open()的问题
- 服务器提交了协议冲突. Section=ResponseStatusLine
- 超级变态的问题:System.Net.WebException: 基础连接已经关闭: 服务器提交了协议冲突。
{
//Get the assembly that contains the internal class
Assembly aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
if (aNetAssembly != null)
{
//Use the assembly in order to get the internal type for the internal class
Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");
if (aSettingsType != null)
{
//Use the internal static property to get an instance of the internal settings class.
//If the static instance isn't created allready the property will create it for us.
object anInstance = aSettingsType.InvokeMember("Section",
BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { }); if (anInstance != null)
{
//Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not
FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);
if (aUseUnsafeHeaderParsing != null)
{
aUseUnsafeHeaderParsing.SetValue(anInstance, true);
return true;
}
}
}
}
return false;
}
listRequest.Proxy = null;
listRequest.Proxy = null;
listRequest.Proxy = null;