HTTP Web请求未返回C#中的预期内容

时间:2009-06-05 12:54:56

标签: c# upload screen-scraping

我目前正致力于将C#应用程序中的文件发布到图像主机(KalleLoad.net - 显然是业主同意)。

我已经获得了工作请求的实际发布,但它没有返回我的预期。上传网站的所有者为我提供了一个API(各种类型),如果我将数据发布到某个地址,它将返回带有URL的XML。我可以成功发布数据并从服务器获得响应,但它只是返回主页的代码而不是XML。我不明白为什么会这样。

我也尝试将数据发布到我本地服务器上的一个简单的PHP页面,它也返回页面的代码,而不是我指示页面在帖子上返回的内容。

以下是我当前发送数据的全部内容。我一直在比较我从我的应用程序发送的标题与firefox在过去半小时内发送的标题,我可以看到它们之间没有真正改变游戏的差异(据我所知)。

任何有关这方面的帮助都会非常棒,而且很受欢迎。

此致 安迪亨特

using System;
using System.Net;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;

namespace Skimpt_3._0
{
    class PostFile
    {
        private Hashtable FormElements;
        private HttpWebRequest Request;
        private MemoryStream FileStream;

        private string CONTENT_BOUNDARY = "---------------------------265001916915724";

        public string ContentMIMEType;
        public string FormURL;
        public string FileName;
        public string Response;
        public string FileBoxName;

        //private int BufferSize;

        public PostFile(string Url, string strFileName)
        {
            FormElements = new Hashtable();
            FormURL = Url;
            Request = (HttpWebRequest)WebRequest.Create(Url);
            //BufferSize = 10240;
            FileStream = new MemoryStream();
            FileName = strFileName;
        }

        public void Send(Image image)
        {
            //Assign the request here too, just in case
            Request = (HttpWebRequest)WebRequest.Create(FormURL);

            Request.Method = "POST";
            Request.AllowWriteStreamBuffering = true;
            Request.ProtocolVersion = HttpVersion.Version11;
            Request.Headers.Add("Cache-Control", "no-cache");
            Request.KeepAlive = true;
            Request.ContentType = "multipart/form-data; boundary=---------------------------265001916915724";
            StartFileStream(FileStream);

            //Must be done in this order for stream to write properly:
            //----
            //Form elements
            //File header
            //Image
            //File trailer
            //----
            WriteStringToStream(FileStream, GetFormElements());
            WriteImageToStream(FileStream, image, FileName);

            CloseStream(FileStream);


            byte[] FileByteArray = FileStream.ToArray();

            Request.ContentLength = FileByteArray.Length;

            Stream PostingStream = Request.GetRequestStream();
            PostingStream.Write(FileByteArray, 0, FileByteArray.Length);

            WebResponse resp = (HttpWebResponse)Request.GetResponse();

            StreamReader SR = new StreamReader(resp.GetResponseStream());

            PostingStream.Close();

            FileStream.Close();
            Request.GetRequestStream().Close();
            Response = SR.ReadToEnd();
            Request = null;
        }

        private void CloseStream(MemoryStream FileStream)
        {
            byte[] BytesToWrite = Encoding.ASCII.GetBytes(CONTENT_BOUNDARY);
            FileStream.Write(BytesToWrite, 0, BytesToWrite.Length);
        }

        private void StartFileStream(MemoryStream FileStream)
        {
            // \r\n = new line
            string str = "POST " + FormURL +"Content-Type: multipart/form-data; boundary="+CONTENT_BOUNDARY+" \r\n \r\n" + CONTENT_BOUNDARY;
            byte[] BytesToWrite = Encoding.ASCII.GetBytes(str);
            FileStream.Write(BytesToWrite, 0, BytesToWrite.Length);
        }

        private Byte[] ConvertImageToByteArray(Image img)
        {
            //Method taken from http://www.csharp-station.com/Articles/Thumbnails.aspx and adapted
            MemoryStream memStream = new MemoryStream();
            img.Save(memStream, System.Drawing.Imaging.ImageFormat.Png);

            byte[] byteArray = new Byte[memStream.Length];

            memStream.Position = 0;
            memStream.Read(byteArray, 0, (int)memStream.Length);
            return byteArray;
        }

        public void AddFormElement(string ElementName, string ElementValue)
        {
            FormElements[ElementName] = ElementValue;
        }

        private string GetFormElements()
        {
            string str = "";
            IDictionaryEnumerator myEnumerator = FormElements.GetEnumerator();
            while (myEnumerator.MoveNext())
            {
                str += CONTENT_BOUNDARY + "\r\n" +
                    "Content-Disposition: form-data; name=" + myEnumerator.Key +
                    "\r\n\r\n" +
                    myEnumerator.Value +"\r\n";
            }
            return str;
        }


        private void WriteStringToStream(System.IO.MemoryStream stream, string String)
        {
            byte[] PostData = System.Text.Encoding.ASCII.GetBytes(String);
            stream.Write(PostData, 0, PostData.Length);
        }

        private void WriteImageToStream(System.IO.MemoryStream Stream, Image img, string FileName)
        {
            byte[] ByteArray = ConvertImageToByteArray(img);
            string head = CONTENT_BOUNDARY + "\r\n" +
                          "Content-Disposition: form-data; name=\"" + FileBoxName + "\"; filename=\"" + FileName + "\"\r\n" +
                          "Content-Type: " + ContentMIMEType + "\r\n\r\n";
            byte[] header = Encoding.ASCII.GetBytes(head);

            Stream.Write(header, 0, header.Length);

            Stream.Write(ByteArray, 0, ByteArray.Length);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

从我可以告诉您提交的POST请求未正确形成。我不认为服务器理解如何解释您发送的数据(因此您没有得到您期望的响应)。

要上传的POST请求应该是什么

POST /_layouts/Upload.aspx HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------7d9192265018e
Host: 127.0.0.1:25540
Content-Length: 3573
Connection: Keep-Alive
---------------------------7d9192265018e
-----------------------------7d9192265018e
Content-Disposition: form-data; name="uploadFile"; filename="C:\Temp\TestDocument.txt"
Content-Type: text/plain

blah
-----------------------------7d9192265018e--

注意:上面的Content-Length值错误:我为了简洁而删除了一些内容。)


您正在生成的POST请求是什么样的

POST /WebSite1/Default.aspx HTTP/1.1
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary=---------------------------265001916915724
Host: 127.0.0.1:25540
Content-Length: 8626
Expect: 100-continue
Connection: Keep-Alive

POST http://ipv4.fiddler:25540/WebSite1/Default.aspxContent-Type: multipart/form-data; boundary=-----------------------------265001916915724 

-----------------------------265001916915724-----------------------------265001916915724
Content-Disposition: form-data; name=""; filename="Test.png"
Content-Type: 

?PNG
-----------------------------265001916915724--

注意:我已经省略了我测试过的PNG文件数据)


通过比较两者,我可以看到以下问题:

  • 边界使用不正确。你在所有地方使用相同的字符串。使用Content-Type标头设置边界值(例如:boundary=aaaBBBcccDDDeee)后,使用前缀为“ - ”的边界(例如: - boundary=aaaBBBcccDDDeee)来分隔多部分表单的每个部分提交。最后的边界标记了多部分数据的结尾,并且必须用“ - ”前缀和后缀分隔(例如:--boundary=aaaBBBcccDDDeee--。有关详细信息,请参阅RFC 1341 (MIME): 7
  • 您的请求中有一个奇怪的Expect: 100-continue标头。这看起来像.NET HttpWebRequest类的问题,可以禁用。有关详情,请参阅HttpWebRequest and the Expect: 100-continue Header Problem
  • StartFileStream方法正在尝试写出标头值,但您将其输出放在请求正文中。它也错误地写出标题。我认为这应该完全省略(看起来它改编自其他一些上传代码)。
  • 字段ContentMIMEType应设置为您要上传的图片的MIME类型。它根本没有设置,因此您上传的图片的内容类型:为空。
  • 同上[{1}}。

如何调试

您可以使用Fiddler查看发送的请求。要进行设置,您需要能够向Web服务器发送请求。您应该能够运行它并拦截您尝试发送的任何请求。

呼!那很长。祝你好运,我希望有所帮助!