Exemple #1
0
 /// <summary>
 /// 触发HTTP请求完成方法
 /// </summary>
 /// <param name="e">事件参数</param>
 public void OnHttpCompleted(WP7HttpEventArgs e)
 {
     if (this._httpCompleted != null)
     {
         this._httpCompleted(this, e);
     }
 }
Exemple #2
0
        /// <summary>
        /// 异步响应回调函数
        /// </summary>
        /// <param name="asyncResult">异步请求参数</param>
        private void handleResponse(IAsyncResult asyncResult)
        {
            string result  = "";
            bool   iserror = false;

            try
            {
                HttpWebRequest  webRequest   = asyncResult.AsyncState as HttpWebRequest;
                HttpWebResponse webResponse  = (HttpWebResponse)webRequest.EndGetResponse(asyncResult);
                Stream          streamResult = webResponse.GetResponseStream(); //获取响应流
                StreamReader    reader       = new StreamReader(streamResult);
                result = reader.ReadToEnd();

                //if (RequestMethod == RequestType.GET && DownloadPath != null)
                //{
                //    IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
                //    myStore.CreateDirectory(Util.getParentPath(Util.ROOT_PATH+DownloadPath.Replace("/","\\"),"\\").Trim());

                //    byte[] bytes = Util.ReadToEnd(streamResult);
                //    using (IsolatedStorageFileStream stream = myStore.CreateFile(Util.ROOT_PATH+DownloadPath.Replace("/","\\")))
                //    {
                //        stream.Write(bytes, 0, bytes.Length);
                //    }

                //}
            }
            catch (WebException ex)
            {
                HttpWebResponse response = ((HttpWebResponse)ex.Response);
                int             code     = (int)response.StatusCode;
                try
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            result = reader.ReadToEnd();
                        }
                    }
                }
                catch (WebException e)
                {
                    UtilDebug.ShowException(e, null, null);
                }

                iserror = true;
            }
            finally
            {
                WP7HttpEventArgs e = new WP7HttpEventArgs();
                e.IsError = iserror;
                e.Result  = result;
                //进行异步回调操作
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(delegate()
                {
                    OnHttpCompleted(e);
                });
            }
        }
Exemple #3
0
        /// <summary>
        /// 新增文件、文件夹
        /// </summary>
        /// <param name="path">文件路径,文件夹后跟“/”</param>
        /// <param name="mount">mout=gokuai 删除该用户用的存储文件</param>
        /// <param name="filehash">文件hash</param>
        /// <param name="overwrite">上传有同名文件是否覆盖,不传默认0 0覆盖,1新建</param>
        /// <param name="stream"></param>
        /// <param name="handler"></param>
        private void Add(string path, string mount, string filehash, int overwrite, Stream stream, WP7HttpRequest.HttpResquestEventHandler handler)
        {
            long filesize = 0;

            if (stream != null)
            {
                filesize = stream.Length;
                if (filesize > 20 * 1024 * 1024)
                {
                    WP7HttpEventArgs e = new WP7HttpEventArgs();
                    e.Result  = "上传文件超出20MB";
                    e.IsError = true;
                    handler.Invoke(this, e);
                    return;
                }
            }

            string signMethod = "add";
            string sign       = GenerateSign(signMethod, path);
            string encodePath = UrlEncoder.Encode(path);

            List <KeyValuePair <string, string> > paramList = new List <KeyValuePair <string, string> >();

            paramList.Add(new KeyValuePair <string, string>("path", encodePath));
            paramList.Add(new KeyValuePair <string, string>("filehash", filehash));
            paramList.Add(new KeyValuePair <string, string>("token", _token));
            paramList.Add(new KeyValuePair <string, string>("filesize", filesize.ToString()));
            paramList.Add(new KeyValuePair <string, string>("overwrite", overwrite.ToString()));
            paramList.Add(new KeyValuePair <string, string>("sign", UrlEncoder.Encode(sign)));

            WP7HttpRequest request = new WP7HttpRequest();

            request._httpCompleted += new WP7HttpRequest.HttpResquestEventHandler(handler);
            request.RequestMethod   = RequestType.PUT;
            request.UploadStream    = stream;
            request.RequestUrl      = URL_API + GenerateUrl(signMethod, paramList);
            request.Request();
        }