首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Windows Mobile >

奇怪的上传文件“空异常”

2012-02-07 
奇怪的上传文件“空错误”最近想写一个WP7上的文件上传软件,但是遇到了问题,主要在于异步上传后EndGetRespon

奇怪的上传文件“空错误”
最近想写一个WP7上的文件上传软件,但是遇到了问题,主要在于异步上传后EndGetResponse时候出错,错误类型很诡异,错误描述是空

WP7代码如下

C# code
    public class upTest    {        RequestInfo m_requestInfo = new RequestInfo();        IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();        public upTest()        {        }        public void start()        {            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://192.168.1.118/ajax/uptest.ashx")) as HttpWebRequest;            request.Method = "POST";            IAsyncResult asyncResult = request.BeginGetRequestStream(new AsyncCallback(RequestStreamCallBack), request);        }        public void Start(string fileName)        {            string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");            byte[] boundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + strBoundary + "\r\n");            //请求头部信息            StringBuilder sb = new StringBuilder();            sb.Append("--");            sb.Append(strBoundary);            sb.Append("\r\n");            sb.Append("Content-Disposition: form-data; name=\"");            sb.Append("file");            sb.Append("\"; filename=\"");            sb.Append(System.IO.Path.GetFileName(fileName));            sb.Append("\"");            sb.Append("\r\n");            sb.Append("Content-Type: ");            sb.Append("application/octet-stream");            sb.Append("\r\n");            sb.Append("\r\n");            string strPostHeader = sb.ToString();            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://192.168.1.118/ajax/uptest.ashx"));            request.Method = "POST";            m_requestInfo.Boundary = strBoundary;            m_requestInfo.FullName = fileName;            m_requestInfo.PostHeader = strPostHeader;            IAsyncResult asyncResult = request.BeginGetRequestStream(new AsyncCallback(RequestStreamCallBack), request);        }        private void RequestStreamCallBack(IAsyncResult ar)        {            HttpWebRequest request = ar.AsyncState as HttpWebRequest;            Stream requestStream = request.EndGetRequestStream(ar);            request.ContentType = "multipart/form-data; boundary=" + m_requestInfo.Boundary;            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(m_requestInfo.PostHeader);            byte[] boundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + m_requestInfo.Boundary + "\r\n");            IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(m_requestInfo.FullName, FileMode.Open, isf);            // Write the post header            requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);                        // Stream the file contents in small pieces (4096 bytes, max).            StreamWriter sw = new StreamWriter(requestStream);            byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)isfs.Length))];            int bytesRead = 0;            while ((bytesRead = isfs.Read(buffer, 0, buffer.Length)) != 0)                 requestStream.Write(buffer, 0, bytesRead);                    // Add the trailing boundary            requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);                        isfs.Close();            request.BeginGetResponse(new AsyncCallback(ResponseCallBack), request);        }        private void ResponseCallBack(IAsyncResult ar)        {            HttpWebRequest request = ar.AsyncState as HttpWebRequest;            WebResponse response = null;            try            {                response = request.EndGetResponse(ar);                StreamReader responseReader = new StreamReader(response.GetResponseStream());                string strResponse = responseReader.ReadToEnd();            }            catch (Exception ex)            {                string strErr = ex.Message;            }                        /*Dispatcher.BeginInvoke(() =>            {                textBox1.Text = responseReader.ReadToEnd();            });*/        }    } 




服务页面代码如下

C# code
public void ProcessRequest (HttpContext context) {                string UpLoadPath;                StringBuilder sb = new StringBuilder();        UpLoadPath = HttpContext.Current.Server.MapPath("..\\") + "\\images";        if (!Directory.Exists(UpLoadPath))        {            Directory.CreateDirectory(UpLoadPath);        }        HttpContext.Current.Response.Write("End Here"); return;        if (HttpContext.Current.Request.Files.Count > 0)        {            HttpPostedFile hpf = HttpContext.Current.Request.Files[0];            string tmpFile;            tmpFile = UpLoadPath + "\\" + hpf.FileName;            if (!File.Exists(tmpFile))            {                File.Delete(tmpFile);            }            hpf.SaveAs(tmpFile);//保存文件            String unicodestring = "success:" + hpf.FileName;            HttpContext.Current.Response.Write(unicodestring);        }    else    {        HttpContext.Current.Response.Write("No Files");    }    }     public bool IsReusable {        get {            return false;        }    }




[解决办法]
InnerException说明是NotSupportedException。
跟踪看是哪个函数导致的?
[解决办法]
第一感觉,你在private void RequestStreamCallBack(IAsyncResult ar)
{
HttpWebRequest request = ar.AsyncState as HttpWebRequest;
///
request.BeginGetResponse(new AsyncCallback(ResponseCallBack), request);

}
我觉得你把程序中的REQUEST都不要用同一个引用试试.我感觉这有隐患 .

热点排行