2

我已经搜索了如何使用控制台应用程序将文件上传到网站,我找到了一些方法,似乎是正确的方法,但我没有成功。所以我需要一些帮助!

首先,我建立的解决方案如下:

class Program
{
    static void Main(string[] args)
    {
        //Program.Test1();
        Program.Test3();
        Console.ReadLine();
    }

    public static void Test3()
    {
        //Set this to dont get an Invalid Request Exception
        System.Net.ServicePointManager.Expect100Continue = false;

        //Set a real page for this test
        string url = "http://www.toledorocket.com/perftest/uploadtest/fileselect.asp";
        string[] files = { "C:\\Documents and Settings\\wkurten\\Desktop\\fogao.txt" }; //Put some real file
        NameValueCollection nvc = new NameValueCollection();
        nvc.Add("FILE1", "fogao.txt");

        string boundary = "----------------------------" +
        DateTime.Now.Ticks.ToString("x");

        HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest2.ContentType = "multipart/form-data; boundary=" +
        boundary;
        httpWebRequest2.Method = "POST";
        httpWebRequest2.KeepAlive = true;
        httpWebRequest2.Credentials =
        System.Net.CredentialCache.DefaultCredentials;

        //Is you have an connection with proxy, uncomment and set the values bellow:
        /*NetworkCredential localNetworkCredential = new NetworkCredential("user", "pass", "domain");
        httpWebRequest2.Proxy = new WebProxy("server:port", false);
        httpWebRequest2.Proxy.Credentials = localNetworkCredential;*/

        Stream memStream = new System.IO.MemoryStream();

        byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
        boundary + "\r\n");

        string formdataTemplate = "\r\n--" + boundary +
        "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";

        foreach (string key in nvc.Keys)
        {
            string formitem = string.Format(formdataTemplate, key, nvc[key]);
            byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
            memStream.Write(formitembytes, 0, formitembytes.Length);
        }

        memStream.Write(boundarybytes, 0, boundarybytes.Length);

        string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";

        for (int i = 0; i < files.Length; i++)
        {
            string header = string.Format(headerTemplate, "file" + i, files[i]);
            //string header = string.Format(headerTemplate, "uplTheFile", files[i]);

            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

            memStream.Write(headerbytes, 0, headerbytes.Length);

            FileStream fileStream = new FileStream(files[i], FileMode.Open,
            FileAccess.Read);
            byte[] buffer = new byte[1024];

            int bytesRead = 0;

            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                memStream.Write(buffer, 0, bytesRead);

            }

            memStream.Write(boundarybytes, 0, boundarybytes.Length);

            fileStream.Close();
        }

        httpWebRequest2.ContentLength = memStream.Length;

        Stream requestStream = httpWebRequest2.GetRequestStream();

        memStream.Position = 0;
        byte[] tempBuffer = new byte[memStream.Length];
        memStream.Read(tempBuffer, 0, tempBuffer.Length);
        memStream.Close();
        requestStream.Write(tempBuffer, 0, tempBuffer.Length);
        requestStream.Close();

        //Gets the response
        WebResponse webResponse2 = httpWebRequest2.GetResponse();

        Stream stream2 = webResponse2.GetResponseStream();
        StreamReader reader2 = new StreamReader(stream2);

        //Retrieve the html from response
        string htmlResponse = reader2.ReadToEnd();

        webResponse2.Close();
        httpWebRequest2 = null;
        webResponse2 = null;
    }

}

我在使用此解决方案时遇到的问题是,当我上传文件并检索 WebResponse 时,我得到的页面是带有上传表单的页面,而不是上传后出现的页面,该页面成功信息。

在我尝试上传文件的页面上,我有这个 html/form:

<FORM METHOD="POST" ENCTYPE="multipart/form-data" ACTION="uploadstatus.asp">
  <INPUT TYPE="FILE" SIZE="40" NAME="FILE1"> <INPUT TYPE=SUBMIT VALUE="Upload!">
</FORM>

当我运行我的代码时,我每次都会得到相同的表单,好像我从来没有发布任何东西......有人知道发生了什么吗?

4

0 回答 0