c#调用Httpget请求
时间: 2023-09-26 09:10:52 浏览: 250
在C#中调用HttpGet请求可以使用以下代码示例:
```csharp
// 引用System.Net命名空间
using System.Net;
public string HttpGet(string url)
{
// 创建WebRequest对象
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
// 设置请求头部
request.ContentType = "text/html, application/xhtml+xml, */*";
// 发送请求并获取响应
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream rs = response.GetResponseStream();
// 读取响应数据
StreamReader sr = new StreamReader(rs, Encoding.UTF8);
string result = sr.ReadToEnd();
// 关闭StreamReader和Stream
sr.Close();
rs.Close();
// 返回响应数据
return result;
}
```
以上代码是一个简单的C#函数,用于发起一个HttpGet请求,并返回响应数据。你可以将需要发送的URL作为参数传递给该函数,它将返回一个包含响应数据的字符串。这个函数使用了System.Net命名空间中的相关类和方法来发送和接收Http请求。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [C# 实现HttpGet请求](https://blog.csdn.net/zq9955/article/details/112234498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [C#调用Get请求](https://blog.csdn.net/qq_36263134/article/details/90444782)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [C# WebApi Get请求方式传递实体参数的方法示例](https://download.csdn.net/download/weixin_38592847/12749834)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文