private void btnWebClientGet_Click(object sender, EventArgs e) { NameValueCollection nvc = GetNameValueCollection(); try { using (WebClient wc = new WebClient()) { wc.Encoding = Encoding.UTF8; if (!string.IsNullOrEmpty(txtUserAgent.Text)) { wc.Headers.Add(HttpRequestHeader.UserAgent, txtUserAgent.Text); } else { wc.Headers.Add(HttpRequestHeader.UserAgent, "WebClient"); } byte[] res = wc.DownloadData(txtUrl.Text + CustHttpSvc.ToQueryString(nvc)); WriteLog("# 資料已使用 GET 送出,伺服器回覆:{0}", Encoding.UTF8.GetString(res)); } } catch (WebException ex) { WriteLog(Color.Red, "# 資料無法使用 GET 送出,原因:"); WriteLog(Color.Red, "# {0}", ex.Message); } catch (HttpException ex) { WriteLog(Color.Red, "# 資料無法使用 GET 送出,原因:"); WriteLog(Color.Red, "# {0}", ex.Message); } }
private void WebRequestCallback(IAsyncResult ar) { if (_HttpListener == null) { return; } try { HttpListenerContext ctx = null; ctx = _HttpListener.EndGetContext(ar); _HttpListener.BeginGetContext(new AsyncCallback(WebRequestCallback), _HttpListener); CustHttpSvc http = new CustHttpSvc(ctx, _SvcName); http.OnPopupMessage += new CustHttpSvc.PopupMessageHandler(delegate(object sender, string msg) { Task.Factory.StartNew(() => WriteLog(Color.Blue, msg)); }); //http.OnReciveAPI += new HttpService.APIHandler(HttpService_OnReciveAPI); http.ProcessRequest(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }