【问题标题】:HTTP web request not returning what is expected in C#HTTP Web 请求未返回 C# 中的预期内容
【发布时间】:2010-10-31 15:19:31
【问题描述】:

我目前正在将文件从 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);
        }
    }
}

【问题讨论】:

  • 我现在正在阅读您的代码,我确实注意到了一些与网络响应无关的内容。对于您将图像转换为 byte[] 的方法,在执行 img.Save(....) 后,您可以简单地返回 memStream.ToArray(),它将返回图像的 byte[]。
  • 谢谢,我现在已经解决了。我不会费心在上面的代码中更新它,因为它真的不需要更新

标签: c# upload screen-scraping


【解决方案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 类型。它根本没有设置,所以你上传的图片的 Content-Type: 是空的。
  • FileBoxName 字段同上。

如何调试

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

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

【讨论】:

  • 感谢您的回复。我会去看看你建议的所有事情。至于变量 ContentMIMEType 和 FileBoxName,它们是在类之外设置的。 IE Instance.ContentMIMEType = "image/png";
  • 效果很好,谢谢。原来我需要的只是每次在内容边界前添加双破折号(“--”)。再次,非常感谢你。这几天一直困扰着我。
  • 好的,现在可以了,但只是部分。出于某种原因,它会上传,但只是部分上传。它不会正确发送 MIME 类型或大小或任何这些属性。有什么想法吗?
  • 嗨,安迪。听起来你已经取得了一些进展!我只能建议使用调试器检查代码并使用我链接到的 Fiddler 工具来查看您发送的请求类型。您应该能够追踪缺失值并相应地修复您的代码。当我看到这个时,我不禁想到必须有一种更简单的方法来做到这一点(不幸的是,虽然不是我经常使用的东西)。
  • 谢谢,dariom。我会看看那个 Fiddler 工具,看看出了什么问题。
猜你喜欢
  • 2020-08-27
  • 2017-02-25
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
  • 1970-01-01
  • 2015-09-27
  • 2018-04-24
  • 2019-01-29
相关资源
最近更新 更多