C#模拟发送http get、post请求的方式
来源:互联网 发布:如何将链接在淘宝打开 编辑:程序博客网 时间:2024/05/16 19:23
1、WebRequest
同步方式:
/// <summary> /// 向指定URL发起请求(可用于远程发送数据) /// </summary> /// <param name="url"> /// <param name="data">发送的数据 /// <param name="method"> /// <returns></returns> public static String RequestUrl(String url, byte[] data, String method = "POST") { try { WebRequest request = WebRequest.Create(url); request.Method = method; request.ContentType = "application/x-www-form-urlencoded"; if (data != null && data.Length > 0) { request.ContentLength = data.Length; Stream newStream = request.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); } else { request.ContentLength = 0; } WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); MemoryStream ms = new MemoryStream(); long ChunkSize = 1024; byte[] buffer = new byte[ChunkSize]; long dataLengthToRead = response.ContentLength;//获取响应数据的总大小 while (dataLengthToRead > 0) { int lengthRead = stream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小 ms.Write(buffer, 0, lengthRead); dataLengthToRead = dataLengthToRead - lengthRead; } stream.Close(); response.Close(); string responseText = Encoding.UTF8.GetString(ms.ToArray()); return responseText; } catch (Exception ex) { return ex.Message; } }
异步方式:
private void Button_Click(object sender, RoutedEventArgs e) { string baseUri =String.Format("http://localhost:49280/Images/{0}.jpg",this.numTextBox.Text.Trim()); HttpWebRequest request =(HttpWebRequest) WebRequest.Create(baseUri); request.Method = "GET"; request.BeginGetResponse(new AsyncCallback(ReadCallback), request); } public void ReadCallback(IAsyncResult asyc) { HttpWebRequest request = (HttpWebRequest)asyc.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyc); this.Dispatcher.BeginInvoke(() => { Stream stream = response.GetResponseStream(); BitmapImage bitmap = new BitmapImage(); bitmap.SetSource(stream); this.image1.Source = bitmap; } ); }
2、WebClient
同步、异步方式
<pre name="code" class="csharp"> using (var client = new WebClient()) { //发送参数 NameValueCollection values = new NameValueCollection(); //请求路径 string url = ConfigurationManager.AppSettings["MerUrl"]; //同步发送form表单请求 byte[] retVal = client.UploadValues(url, "POST", values);
<span style="white-space:pre"></span> //通过UploadValuesCompleted事件可绑定请求完成时的调用方法
//获取返回结果 result = JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(retVal)); }
0 0
- C#模拟发送http get、post请求的方式
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送POST或GET请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#发送Post方式的Http请求
- java模拟发送http的get、post请求
- java使用HttpClient模拟发送http的get、post请求
- Java发送HTTP的POST和GET方式请求
- c#发送http请求get post
- C#模拟Post和Get方式发送数据的技巧
- HttpURLConnection抛异常java.io.FileNotFoundException
- View绘制流程简介
- Android 开发-Shape相关
- 码农们可以优越,并且应该优越
- 【CCF】ISBN号码
- C#模拟发送http get、post请求的方式
- PageRank Spark implementation
- 机器人
- 受限玻尔兹曼机(RBM,Restricted Boltzmann Machines)浅介
- VS2010 MFC的小Bug-ASSERT(ContinueModal());
- MSSql Server基础学习系列———数据检索
- vim安装nerdtree插件
- 【CSS】CSS 私家库
- soapui中文操作手册(九)----REST Sample Project