问题描述
我收到此错误:
I get this error:
"System.dll中发生了类型为'System.Net.WebException'的未处理的异常
"An unhandled exception of type 'System.Net.WebException' occurred in System.dll
附加信息:所请求的URI对于此FTP命令无效."
Additional information: The requested URI is invalid for this FTP command."
我仍无法发布屏幕截图,但是它发生在以下行:"Stream requestStream = request.GetRequestStream();"
I cant post a screenshot yet but it occurs for this line: "Stream requestStream = request.GetRequestStream();"
该程序读取和/或写入文本文件,然后上传到ftp.
The program reads and/or writes a text file and uploads to ftp.
我正在从msdn站点使用此代码:
I am using this code from the msdn site:
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main ()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","[email protected]");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader("testfile.txt");
byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
}
}
}
这是完整的代码,少了登录凭据.
Here is the full code less login credentials.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Net;
namespace Streamer
{
public partial class Form1 : Form
{
private string User = "user";
private string Password = "password";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.ShowDialog();
textBox1.Text = of.FileName;
}
private void button2_Click(object sender, EventArgs e)
{
StreamReader sr = new StreamReader(textBox1.Text);
textBox2.Text = sr.ReadToEnd();
sr.Close();
}
private void button3_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(textBox1.Text, false);//writeToFileLocation
sw.WriteLine(textBox3.Text);//writeFrom
sw.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
// StreamWriter sw = new StreamWriter(textBox1.Text, false);//writeToFileLocation
// sw.WriteLine(textBox3.Text);//writeFrom
//sw.Close();
// Get the object used to communicate with the server.
// Get the object used to communicate with the server.
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://files.000webhost.com/public_html/members/zones/" + textBox3.Text.Trim());
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential(this.User, this.Password);
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader("streamer.txt");
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
//
}
}
}
推荐答案
>>>>" FtpWebRequest request =(FtpWebRequest)WebRequest.Create(< " + textBox3.Text.Trim());
>>"FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://files.000webhost.com/public_html/members/zones/" + textBox3.Text.Trim());
感谢您的信息. textBox3的值是什么?上传FTP文件需要一个以文件名结尾的URL.因此您的网址应该是这样.
Thank you for your post. What is the value of textBox3? FTP file upload need a URL which end with a file name. So your URL should be like this.
ftp://files.000webhost.com/public_html/members/zones/streamer.txt
最好的问候,
王丽
Best Regards,
Li Wang
这篇关于帮助上传到FTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!