none
wp8中HttpWebRequest.EndGetRequestStream报“Value does not fall within the expected ”,求帮忙! RRS feed

  • 问题

  • 在使用HttpWebRequest,method为post进行上传文件。上传其中的一部分,比如大文件,上传2048K的时候上传成功了,但在回调的地方HttpWebRequest.EndGetRequestStream的时候就报“Value does not fall within the expected “错误了!不知道怎么解了!

    下面是部分代码

      httpReq = (HttpWebRequest)WebRequest.Create(new Uri(strUri));
      httpReq.Method = "Post";

      httpReq.ContentLength = 2048;

     IAsyncResult result2 = httpReq.BeginGetRequestStream(ReadCallback, httpReq);

      private void ReadCallback(IAsyncResult asynchronousResult)
       {
                var request = (HttpWebRequest)asynchronousResult.AsyncState;

                using (var postStream = request.EndGetRequestStream(asynchronousResult))//到这里就报如题的错误了(此时抓包看到返回的为200,已经是OK了的)
                {
                    using (var writer = new System.IO.BinaryWriter(postStream))
                    {

                     }

               }

    }

    目前设置的httpReq.ContentLength = 2048;为2048,设置成这个文件的总大小也是一样出这个问题!如何解决啊!要疯了!

    2014年7月21日 3:35

全部回复

  • 在使用HttpWebRequest执行上传文件的时候, IAsyncResult result2 = httpReq.BeginGetRequestStream(ReadCallback, httpReq);

    //在ReadCallBack中会出现写文件报错的问题

     private void ReadCallback(IAsyncResult asynchronousResult)
           {
                var request = (HttpWebRequest)asynchronousResult.AsyncState;

                using (var postStream = request.EndGetRequestStream(asynchronousResult))
                {
                    using (var writer = new System.IO.BinaryWriter(postStream))
                    {
                        try
                        {
                            IsolatedStorageFileStream CurrentNeedUpdateStream = null;
                            if (IsolatedStorageFile.GetUserStoreForApplication().FileExists(m_CurrentFileName))
                            {
                                CurrentNeedUpdateStream = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(m_CurrentFileName, FileMode.Open);
                            }

                            long lCount = CurrentNeedUpdateStream.Seek(long.Parse(m_CurrentPosition), SeekOrigin.Begin);
                            byte[] buffer = new byte[CurrentNeedUpdateStream.Length];
                            CurrentNeedUpdateStream.Read(buffer, 0, buffer.Length);

                            StringBuilder stringBuilder = new StringBuilder();
                            stringBuilder.Append("--");
                            stringBuilder.Append(m_strBoundary);
                            stringBuilder.Append("\r\n");
                            stringBuilder.Append("Content-Dis-data; name=\"");
                            stringBuilder.Append("Filename");
                            stringBuilder.Append("\"");
                            stringBuilder.Append("\r\n");
                            stringBuilder.Append("\r\n");
                            stringBuilder.Append(m_CurrentRealyFileName);
                            stringBuilder.Append("\r\n");
                            stringBuilder.Append("--");
                            stringBuilder.Append(m_strBoundary);
                            stringBuilder.Append("\r\n");
                            stringBuilder.Append("Content-Dis-data; name=\"");
                            stringBuilder.Append("file1");
                            stringBuilder.Append("\"; filename=\"");
                            stringBuilder.Append(m_CurrentRealyFileName);
                            stringBuilder.Append("\"");
                            stringBuilder.Append("\r\n");
                            stringBuilder.Append("Content-Type: ");
                            stringBuilder.Append("application/octet-stream");
                            stringBuilder.Append("\r\n");
                            stringBuilder.Append("\r\n");

                            string strBuilder = stringBuilder.ToString();
                            writer.Write(strBuilder);
                           
                            writer.Write(buffer);  //到这里就会报错,下面将当前buffer的内容截图,及报错截图发上,希望有人能帮忙看下,怎么解决这个问题
                        }
                        catch (System.Exception ex)
                        {
                         
                        }
                    }

    buffer的内容截图

    出错的截图

    在线等待

    2014年7月16日 7:21
  • Billy 你好,

    在我这边没法重现这个问题,我对这个错误信息进行了调查,请参考一下:

    1. 错误信息具体内容:

    http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse(v=vs.110).aspx

    可以根据异常信息来检查一下你的代码;

    2. 参考这个帖子:

    http://stackoverflow.com/questions/3890492/c-getting-request-stream-of-httpwebrequest-gives-protocolviolationexception

    修改 Request 的 Method;

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年7月17日 2:37
    版主
  • 谢谢 Jimmy!通过你的提示,我找到了我的问题所在。问题出在我在发送request前进行了设置了httpReq.AllowWriteStreamBuffering = false;导致了这个问题,提出来,方便以后遇到的人把!

    现在这个问题解决了,但我上传成功了,在ResponseCallback中却出问题了,我本想通过这里来获取上传的进度值的,但现在不知道怎么获取这个进度了。

    我先使用httpReq.BeginGetRequestStream(ReadCallback, httpReq);来请求,

    后面也是使用 request.BeginGetResponse(ResponseCallback, request);来检测响应  (网站写了使用了BeginGetRequestStream,就得使用BeginGetResponse来检测响应)

    但在ResponseCallback中会报这个错误

      private void ResponseCallback(IAsyncResult asynchronousResult)
            {

            //想制作出上传进度,不知道上传的进度值是不是在这里可以获取到??

                var request = (HttpWebRequest)asynchronousResult.AsyncState;
               
                try
                {
                    using (var resp = (HttpWebResponse)request.EndGetResponse(asynchronousResult))  //运行到这里就出错了,
                    {
                        using (var streamResponse = resp.GetResponseStream())
                        {

                            using (var streamRead = new StreamReader(streamResponse))
                            {
                               // string responseString = streamRead.ReadToEnd();


                                // do something with responseString to check if login was successful

                            }
                        }
                    }
                }

    还请jimmy赐教下!

    2014年7月17日 8:27
  • Billy 你好,

    通过这种异步调用是没办法获得进度信息的。这是第一代的异步编程(APM). 建议使用Backgroundworker组件来实现进度的控制,很早之前我写过一个WinForm的,你可以参考一下,下载地址:

    https://onedrive.live.com/redir?resid=F6BFF8BD172F0399%21240

    希望对你有帮助!

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年7月18日 5:43
    版主
  • 谢谢!Jimmy! 我之前是使用BackgroundService的方式来上传的,是可以做到进度值。但BackgroundService限制太多,既限制大小,又会出现电量不足,提示WIFI 等情况。所以改换成限制的HttpWebRequest。  看来这样是没办法获取到进度值了。

    那出现这个错误是什么原因呢?我一直没找到原因,都不知道怎么解决这个问题了、

    2014年7月18日 8:16
  • Billy 你好,

    这个错误发生的原因应该是异步使用不正确造成的,建议参考下面两个帖子:

    http://stackoverflow.com/questions/19904334/an-exception-of-type-system-net-webexception-occurred-in-system-windows-ni-dll

    http://stackoverflow.com/questions/19034199/properly-handling-httpclient-exceptions-within-async-await

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年7月21日 3:25
    版主
  • 你在非WP程序上先试试这种上传和下载。这样也方便调试。
    2014年7月23日 5:14
  • 这种方式上传40M一下的文件是正常,可以OK成功,但40M以上就会出现内存溢出问题。所以我才想把大文件进行分包进行上传。

    还有就是非WP程序,就用不了我这里的代码,但思想是一直的。WP里全是异步的,而非WP都是使用同步的方式。

    2014年7月24日 7:03