Http 请求

Posted 每天进步一点点!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Http 请求相关的知识,希望对你有一定的参考价值。

 1 public static string HttpGet(string url)
 2         {
 3             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
 4             request.Method = "GET";//设置请求的方法
 5             request.Accept = "*/*";//设置Accept标头的值
 6             string responseStr = "";
 7             using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())//获取响应
 8             {
 9                 using (StreamReader reader =
10         new StreamReader(response.GetResponseStream(), Encoding.UTF8))
11                 {
12                     responseStr = reader.ReadToEnd();
13                 }
14             }
15             return responseStr;
16         }

 

 1  public static string HttpPost(string url, string  data)
 2         {
 3             //当请求为https时,验证服务器证书
 4             ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((a, b, c, d) => true);
 5           var req= (HttpWebRequest)WebRequest.Create(url);
 6             req.Method = "POST";
 7             req.ContentType = "application/x-www-form-urlencoded";
 8             req.Accept = "*/*";
 9             req.Timeout = 15000;
10             req.AllowAutoRedirect = false;
11             string responseStr = "";
12             using (var writer = new StreamWriter(req.GetRequestStream()))
13             {
14                writer.Write(data);
15             }
16             using (var  res = (HttpWebResponse)req.GetResponse())
17             {
18                 using (var reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8))
19                 {
20                     responseStr = reader.ReadToEnd();//获取响应
21                 }
22             }
23             return responseStr;
24         }

 

 

 

 1   public static string HttpPost(string url, string stream)
 2         {
 3             //当请求为https时,验证服务器证书
 4             ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((a, b, c, d) => true);
 5             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
 6             request.Method = "POST";
 7             request.ContentType = "application/x-www-form-urlencoded";
 8             request.Accept = "*/*";
 9             request.Timeout = 15000;
10             request.AllowAutoRedirect = false;
11             string responseStr = "";
12             using (var reqstream = request.GetRequestStream())
13             {
14                 stream.Position = 0L;
15                 stream.CopyTo(reqstream);
16             }
17             using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
18             {
19                 using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
20                 {
21                     responseStr = reader.ReadToEnd();//获取响应
22                 }
23             }
24             return responseStr;
25         }

 

 1 public static string HttpPostForm(string url, List<FormEntity> form)
 2         {
 3             //分割字符串
 4             var boundary = "----" + DateTime.Now.Ticks.ToString("x");
 5             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
 6             request.Method = "POST";
 7             ServicePointManager.ServerCertificateValidationCallback =
 8                new RemoteCertificateValidationCallback((a, b, c, d) => true);
 9             request.ContentType = "multipart/form-data; boundary=" + boundary;
10             MemoryStream stream = new MemoryStream();
11             #region 将非文件表单写入内存流
12             foreach (var entity in form.Where(f => f.IsFile == false))
13             {
14                 var temp = new StringBuilder();
15                 temp.AppendFormat("\r\n--{0}", boundary);
16                 temp.Append("\r\nContent-Disposition: form-data;");
17                 temp.AppendFormat("name=\"{0}\"", entity.Name);
18                 temp.Append("\r\n\r\n");
19                 temp.Append(entity.Value);
20                 byte[] b = Encoding.UTF8.GetBytes(temp.ToString());
21                 stream.Write(b, 0, b.Length);
22             }
23             #endregion
24             #region 将文件表单写入内存流
25             foreach (var entity in form.Where(f => f.IsFile == true))
26             {
27                 byte[] filedata = null;
28                 var filename = Path.GetFileName(entity.Value);
29                 //表示是网络资源
30                 if (entity.Value.Contains("http"))
31                 {
32                     //处理网络文件
33                     using (var client = new WebClient())
34                     {
35                         filedata = client.DownloadData(entity.Value);
36                     }
37                 }
38                 else
39                 {
40                     //处理物理路径文件
41                     using (FileStream file = new FileStream(entity.Value,
42                     FileMode.Open, FileAccess.Read))
43                     {
44                         filedata = new byte[file.Length];
45                         file.Read(filedata, 0, (int)file.Length);
46                     }
47                 }
48                 var temp = string.Format("\r\n--{0}\r\nContent-Disposition: " +
49                 "form-data; name=\"{1}\"; filename=\"{2}\"\r\n\r\n",
50                    boundary, entity.Name, filename);
51                 byte[] b = Encoding.UTF8.GetBytes(temp);
52                 stream.Write(b, 0, b.Length);
53                 stream.Write(filedata, 0, filedata.Length);
54             }
55             #endregion
56             //结束标记
57             byte[] foot_data = Encoding.UTF8.GetBytes("\r\n--" + boundary +
58             "--\r\n");
59             stream.Write(foot_data, 0, foot_data.Length);
60             Stream reqStream = request.GetRequestStream();
61             stream.Position = 0L;
62             //将Form表单生成流写入请求流
63             stream.CopyTo(reqStream);
64             stream.Close();
65             reqStream.Close();
66             using (HttpWebResponse response = (HttpWebResponse)request.
67             GetResponse())
68             {
69                 using (StreamReader reader =
70        new StreamReader(response.GetResponseStream(), Encoding.UTF8))
71                 {
72                     return reader.ReadToEnd();//获取响应
73                 }
74             }
75         }

 

以上是关于Http 请求的主要内容,如果未能解决你的问题,请参考以下文章

postman 自动生成 curl 代码片段

如何对媒体片段的任何部分进行范围请求?

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装

在哪里以及如何使用片段填充我的标签