/// <summary> /// Downloads the resource at the specified Uri as a string. /// </summary> /// <param name="address">The location of the resource to be downloaded.</param> public void DownloadStringAsync(string address, List<APIParameter> paras, HttpMethod method=HttpMethod.POST) { if (paras == null || paras.Count == 0) throw new ArgumentNullException("The paras required!"); HttpWebRequest request; try { // POST if (method == HttpMethod.POST) { RequestState state = new RequestState(); request = (HttpWebRequest)WebRequest.Create(address); request.Method = HttpMethod.POST.ToString(); request.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; //平台统计用 request.UserAgent = "Renren Windows Phone SDK v2.0 (windows phone; windows phone 7.1)"; state.request = request; state.paras = paras; request.BeginGetRequestStream(new AsyncCallback(RequestReady), state); } else if (method == HttpMethod.GET) { address = ApiHelper.AddParametersToURL(address, paras); request = (HttpWebRequest)WebRequest.Create(address); request.Method = HttpMethod.GET.ToString(); request.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; request.BeginGetResponse(new AsyncCallback(ResponseReady), request); } } catch { if (DownloadStringCompleted != null) { System.Windows.Deployment.Current.Dispatcher.BeginInvoke(delegate() { DownloadStringCompleted(this, new DownloadStringCompletedEventArgs(new Exception("Error creating HTTP web request."))); }); } } }
/// <summary> /// Downloads the resource at the specified Uri as a string. /// </summary> /// <param name="address">The location of the resource to be downloaded.</param> public void DownloadStringAsyncWithFile(string address,List<APIParameter> paras,List<APIParameter> files) { if (paras == null || paras.Count == 0) throw new ArgumentNullException("The paras required!"); if (files == null || files.Count == 0) throw new ArgumentNullException("The files required!"); HttpWebRequest request; RequestState state = new RequestState(); try { request = (HttpWebRequest)WebRequest.Create(address); boundary = DateTime.Now.Ticks.ToString("X"); request.Method = HttpMethod.POST.ToString(); request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary; state.request = request; state.paras = paras; state.files = files; request.BeginGetRequestStream(new AsyncCallback(RequestReadyWithFile), state); } catch { if (DownloadStringCompleted != null) { System.Windows.Deployment.Current.Dispatcher.BeginInvoke(delegate() { DownloadStringCompleted(this, new DownloadStringCompletedEventArgs(new Exception("Error creating HTTP web request."))); }); } } }