C#HTTPPOST请求数据带参数

参数: url: 目标地址

data:要post的数据比如a=1&b=2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public  string  GetPostString( string  url,  string  data)
{
try
{
byte [] postBytes = Encoding.GetEncoding( "utf-8" ).GetBytes(data);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method =  "POST" ;
myRequest.ContentType =  "text/html" ;
myRequest.ContentLength = postBytes.Length;
myRequest.Proxy =  null ;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(postBytes, 0, postBytes.Length);
newStream.Close();
// Get response
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
using  (StreamReader reader =  new  StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding( "utf-8" )))
{
string  content = reader.ReadToEnd();
return  content;
}
}
catch  (System.Exception ex)
{
return  ex.Message;
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.Collections; using System.IO; using System.Text.RegularExpressions; using RE = System.Text.RegularExpressions.Regex; using System.Security.Cryptography.X509Certificates; /*************************************************************************************************************************************************** * *文件名:HttpProc.cs * *创建人:HeDaode * *日 期:2007.09.01 * *描 述:实现HTTP协议中的GET、POST请求 * *使 用:HttpProc.WebClient client = new HttpProc.WebClient(); client.Encoding = System.Text.Encoding.Default;//默认编码方式,根据需要设置其他类型 client.OpenRead("http://www.baidu.com");//普通get请求 MessageBox.Show(client.RespHtml);//获取返回的网页源代码 client.DownloadFile("http://www.codepub.com/upload/163album.rar",@"C:\163album.rar");//下载文件 client.OpenRead("http://passport.baidu.com/?login","username=zhangsan&password=123456");//提交表单,此处是登录百度的示例 client.UploadFile("http://hiup.baidu.com/zhangsan/upload", @"file1=D:\1.mp3");//上传文件 client.UploadFile("http://hiup.baidu.com/zhangsan/upload", "folder=myfolder&size=4003550",@"file1=D:\1.mp3");//提交含文本域和文件域的表单 *****************************************************************************************************************************************************/ namespace HttpProc { /// ///上传事件委托 /// /// /// public delegate void WebClientUploadEvent(object sender, HttpProc.UploadEventArgs e); /// ///下载事件委托 /// /// /// public delegate void WebClientDownloadEvent(object sender, HttpProc.DownloadEventArgs e); /// ///上传事件参数 /// public struct UploadEventArgs { /// ///上传数据总大小 ///
C#中,可以使用`HttpClient`类来发送有header参数和data的POST请求。下面是一个示例代码,展示了如何发送包含header参数和data的数据: ```csharp using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; class Program { static void Main() { // 创建HttpClient对象 var httpClient = new HttpClient(); // 设置请求头信息 var requestHeaders = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("Content-Type", "application/json"), // 设置请求内容类型为JSON new KeyValuePair<string, string>("Authorization", "Bearer your_token_here") // 设置授权信息,这里假设使用Bearer授权方式 }; httpClient.DefaultRequestHeaders.Add(HttpRequestHeader.Headers["Authorization"], "Bearer your_token_here"); // 添加到请求头中 // 创建JSON数据字符串 var data = new Dictionary<string, string> { { "miId", "17095346587741851203" }, { "uId", "17023719687281451428" } }; var jsonData = JsonConvert.SerializeObject(data); // 发送POST请求并获取响应 var responseTask = httpClient.PostAsync("http://example.com/api/endpoint", new StringContent(jsonData, Encoding.UTF8, "application/json")); var response = await responseTask; // 处理响应结果 if (response.IsSuccessStatusCode) { // 处理成功响应数据 Console.WriteLine("POST请求成功"); Console.WriteLine("响应数据: " + response.Content.ReadAsStringAsync().Result); } else { // 处理失败响应数据 Console.WriteLine("POST请求失败"); Console.WriteLine("响应状态码: " + response.StatusCode); Console.WriteLine("响应原因: " + response.ReasonPhrase); } } } ``` 在上面的示例中,我们创建了一个`HttpClient`对象,并设置了请求头信息。然后,我们创建了一个JSON格式的数据字符串,并将其作为POST请求数据发送到指定的URL。最后,我们通过`PostAsync`方法发送POST请求,并使用`await`关键字等待响应。在响应处理部分,我们可以根据实际情况进行相应的处理。请确保替换示例代码中的URL、请求头和数据内容为自己的实际值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值