从 SilverLight 应用程序使用 HTTPWebrequest 上传文件(为什么内容长度为 13?)

发布于 2024-11-16 08:11:41 字数 2480 浏览 3 评论 0原文

朋友们! 请帮助我!

我尝试从 Silverlight 发布文件。我使用这样的类:

public class HttpHelper
    {
        public WebRequest Request { get; set; }
        public Stream Filestream { get; private set; }

        public HttpHelper(Stream filestream)
        {            
            Request = WebRequest.Create("http://www.mysite.com/recieve");                
            Request.Method = "POST";
            Request.ContentType = "application/octet-stream";
            Filestream = filestream;
        }

        private static void BeginFilePostRequest(IAsyncResult ar)
        {
            HttpHelper helper = ar.AsyncState as HttpHelper;
            if (helper != null)
            {
                byte[] bytes = new byte[helper.Filestream.Length];
                int sf = helper.Filestream.Read(bytes, 0, (int)helper.Filestream.Length);
                //helper.Request.ContentLength = bytes.Length; //It doesn't work in SL
                using (StreamWriter writer = new StreamWriter(helper.Request.EndGetRequestStream(ar)))
                {
                    writer.Write(bytes);
                }
                helper.Request.BeginGetResponse(new AsyncCallback(HttpHelper.BeginResponse), helper);
            }
        }

        private static void BeginResponse(IAsyncResult ar)
        {
            HttpHelper helper = ar.AsyncState as HttpHelper;
            if (helper != null)
            {
                HttpWebResponse response = (HttpWebResponse)helper.Request.EndGetResponse(ar);
                if (response != null)
                {
                    Stream stream = response.GetResponseStream();
                    if (stream != null)
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            //anything...
                        }
                    }
                }
            }
        }

        public void PostFile()
        {            
            this.Request.BeginGetRequestStream(new AsyncCallback(HttpHelper.BeginFilePostRequest), this);
        }
    }

我的 silverlight 应用程序中有 Stream 并尝试通过单击提交按钮调用 PostFile:

private void submit_button_Click(object sender, RoutedEventArgs e)
        {           
            //...         
            HttpHelper helper = new HttpHelper(memory_stream);
            helper.PostFile();
        }

但我的网站收到没有文件的请求。它只有 ContentLength 13。有什么问题吗?

Friends!
Help me, please!

I try to post file from Silverlight. I use such class:

public class HttpHelper
    {
        public WebRequest Request { get; set; }
        public Stream Filestream { get; private set; }

        public HttpHelper(Stream filestream)
        {            
            Request = WebRequest.Create("http://www.mysite.com/recieve");                
            Request.Method = "POST";
            Request.ContentType = "application/octet-stream";
            Filestream = filestream;
        }

        private static void BeginFilePostRequest(IAsyncResult ar)
        {
            HttpHelper helper = ar.AsyncState as HttpHelper;
            if (helper != null)
            {
                byte[] bytes = new byte[helper.Filestream.Length];
                int sf = helper.Filestream.Read(bytes, 0, (int)helper.Filestream.Length);
                //helper.Request.ContentLength = bytes.Length; //It doesn't work in SL
                using (StreamWriter writer = new StreamWriter(helper.Request.EndGetRequestStream(ar)))
                {
                    writer.Write(bytes);
                }
                helper.Request.BeginGetResponse(new AsyncCallback(HttpHelper.BeginResponse), helper);
            }
        }

        private static void BeginResponse(IAsyncResult ar)
        {
            HttpHelper helper = ar.AsyncState as HttpHelper;
            if (helper != null)
            {
                HttpWebResponse response = (HttpWebResponse)helper.Request.EndGetResponse(ar);
                if (response != null)
                {
                    Stream stream = response.GetResponseStream();
                    if (stream != null)
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            //anything...
                        }
                    }
                }
            }
        }

        public void PostFile()
        {            
            this.Request.BeginGetRequestStream(new AsyncCallback(HttpHelper.BeginFilePostRequest), this);
        }
    }

I have Stream in my silverlight application and try to call PostFile by click submit button:

private void submit_button_Click(object sender, RoutedEventArgs e)
        {           
            //...         
            HttpHelper helper = new HttpHelper(memory_stream);
            helper.PostFile();
        }

But mysite recieve request without file. It just has ContentLength 13. What's problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月朦胧 2024-11-23 08:11:41

在退出 using 块之前尝试对编写器进行 Flush,您还应该对从 EndGetRequestStream 检索到的流调用 Close

Try Flush on your writer before exiting the using block, you should also call Close on the stream retrieved from EndGetRequestStream.

青衫儰鉨ミ守葔 2024-11-23 08:11:41

你必须刷新并处理 HTTP 请求流和所有下游流,然后它才能工作。

you HAVE TO Flush and Dispose HTTP request stream and all downstream streams, then it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文