C#将图像对象读入Web请求

时间:2009-06-02 14:08:06

标签: c# image httpwebrequest

嘿,我正在尝试使用Web请求,内存流和图像对象将图像文件发送到Web服务器。

这是我目前发送请求的方法:

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

        Request.Method = "POST";
        Request.ContentType = "multipart/form-data; boundary=" + CONTENT_BOUNDARY;
        Request.Headers.Add("Cache-Control", "no-cache");
        Request.KeepAlive = true;
        Request.ContentLength = GetFormElements().Length +
        GetFileHeader(FileName).Length +
        FILE_TRAIL.Length +
        ConvertImageToByteArray(image).Length;

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

        string PostData = ASCIIEncoding.ASCII.GetString(ConvertImageToByteArray(image));

        StreamWriter SW = new StreamWriter(Request.GetRequestStream(), Encoding.ASCII);
        SW.Write(PostData);

        GetRequestResponse();
        Console.WriteLine(Request.HaveResponse);
        FileStream.Close();
        Request = null;

    }

我遇到的问题是我根本没有从服务器得到任何响应,尽管流看起来是正确的长度(我从上面的代码中删除了一些调试输出)

如果有必要,我可以发布课程的其他部分,但现在这里是写作函数:

WriteStringToStream:

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

WriteImageToSteam:

private void WriteImageToStream(System.IO.MemoryStream Stream, Image img)
{
    Stream.Write(ConvertImageToByteArray(img), 0, ConvertImageToByteArray(img).Length);
}

ConvertImageToByteArray:

    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.Jpeg);

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

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

4 个答案:

答案 0 :(得分:3)

您应该在发送请求之前关闭SW。

此外,您应该将字节数组直接写入请求流,而不是将字节数组转换为ASCII然后将其写入StreamWriter。 (然后在发送请求之前关闭请求流)

答案 1 :(得分:0)

“FileStream”的目的是什么?声明未显示,您将数据写入,然后关闭它。您的意思是将其写入请求流并忘记了吗?好像你在Request.ContentLength中计算它,但从不将它写入请求。

答案 2 :(得分:0)

        private void GetRequestResponse()
    {
        if (null == FileStream)
        {
            System.IO.Stream stream;
            WebResponse webResponse;
            try
            {
                webResponse = Request.GetResponse();
            }
            catch (WebException web)
            {
                webResponse = web.Response;
            }

            if (null != webResponse)
            {
                stream = webResponse.GetResponseStream();
                StreamReader sr = new StreamReader(stream);
                string str;
                Response = "";

                while ((str = sr.ReadLine()) != null)
                {
                    Response += str;
                }
                webResponse.Close();
            }
            else
            {
                throw new Exception("Error retrieving server response");
            }
        }
    }

响应SLaks,以上是GetRequestResponse的内容。在这个类中,Response只是一个字符串。

答案 3 :(得分:-1)

问题解决了:)我现在有了回应 - 即使它不是我期待的......