C#的post请求
来源:互联网 发布:java内存溢出分析工具 编辑:程序博客网 时间:2024/05/01 21:43
发起请求 :
// 创建WebRequest对像
var request = HttpWebRequest.Create("http://www.yourwebsite.com");// 设置请求的方式为Post
request.Method = "POST";
// 发起获取发送数据流的请求
request.BeginGetRequestStream(ResponseStreamCallbackPost, request);
// 发起获取发送数据流的请求的响应回调方法
private async void ResponseStreamCallbackPost(IAsyncResult result)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)result.AsyncState;
……接下来通过发送数据流来发送数据
}
传递POST数据:
发起Post请求之后在会调的方法里面可以通过HttpWebRequest对象来获取到发送的数据流,把传递的Post数据写入数据流来实现数据的传递。代码示例如下所示:
// 发起获取发送数据流的请求的响应回调方法
private async void ResponseStreamCallbackPost(IAsyncResult result)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)result.AsyncState;
using(Stream stream = httpWebRequest.EndGetRequestStream(result))
{
// 你需要把你要发送的数据通过byte[]数据格式发送出去
// 如下面把字符串的信息转化为byte[]
string postString = "yourstring";
byte[] data = Encoding.UTF8.GetBytes(postString);
stream.Write(data, 0, data.Length);
}
……接下来获取请求的响应
}
获取结果:
Post请求的数据写入完毕的之后,我们需要继续发起BeginGetResponse请求,获取服务器端的相应,这一步骤和Get请求是一致的,服务器需要把相关的结果返回给客户端。代码示例如下所示:
httpWebRequest.BeginGetResponse(ResponseCallbackPost, httpWebRequest);
// 请求回调方法
private async void ResponseCallbackPost (IAsyncResult result)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)result.AsyncState;
WebResponse webResponse = httpWebRequest.EndGetResponse(result);
// 获取请求返回的内容
using (Stream stream = webResponse.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
// 请求返回的字符串内容
string content = reader.ReadToEnd();
}
}
- C#的post请求
- C#发送Post方式的Http请求
- C#获取post请求的json字符串
- C# http post请求
- C#模拟post请求
- c# 通过post请求
- C# Post请求 方法
- C# 发送POST请求
- C# post 请求
- Http请求之--C#的HttpWebRequest实现POST方式请求
- C#请求网页(Get、Post)
- C# WebRequest WebClient Post请求
- 使用C#发送POST请求
- c# 接收手动post请求
- C#模拟网络POST请求
- C# HttpWebRequest提交post请求
- C#请求网页(Get、Post)
- C# 通过接口 post 请求
- 【codeforces 779D】String Game
- LeetCode1. Two Sum
- JetBrains 激活码
- 文章标题
- 深入浅出MySQL(6)-常用数据存储引擎初探
- C#的post请求
- 高精度减法
- Unity3d DLL脚本通用解密方法
- js代码判断浏览器类型(chrom\IE\firfox\safari\)opera)的类型
- hdu 1016Prime Ring Problem
- C++ / QT 对象序列化(Object Serialization)的实现
- PG和Oracle的差异
- 【转载】Best Practices for Speeding Up Your Web Site
- Web 通信 之 长连接、长轮询(long polling)