下面是详细的讲解“c#发送请求访问外部接口的实例”的完整攻略。
1. 创建HttpClient实例
使用HttpClient类发送HTTP请求。HttpClient类位于System.Net.Http命名空间中。可以在项目中添加对System.Net.Http命名空间的引用来使用HttpClient类。
示例代码:
using System.Net.Http;
HttpClient httpClient = new HttpClient();
2. 构建请求对象
构建HTTP请求信息对象,包括URL、请求方法、请求头和请求正文。我们可以使用HttpRequestMessage对象来构建请求信息。
示例代码:
string url = "http://example.com/api";
HttpMethod method = HttpMethod.Get;
HttpRequestMessage requestMessage = new HttpRequestMessage(method, url);
3. 添加请求头(如果需要)
为HTTP请求添加请求头。我们可以使用HttpRequestHeaders对象来添加请求头。
示例代码:
requestMessage.Headers.Add("Accept", "application/json");
4. 发送请求
使用创建HttpClinet实例的SendAsync方法发送HTTP请求,并获取响应结果。SendAsync方法发送HTTP请求并返回响应的一个Task。使用await关键字等待Task执行完成后才能继续执行后续代码。
示例代码:
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
5. 处理响应结果
获取HTTP响应的状态码和响应内容。可以使用HttpResponseMessage对象的StatusCode属性获取HTTP响应的状态码,Content属性获取HTTP响应的内容。在获取响应内容之前,我们要先确认HTTP响应的ContentType。
示例代码:
if (responseMessage.IsSuccessStatusCode)
{
string responseContentType = responseMessage.Content.Headers.ContentType.MediaType;
if (responseContentType.Equals("application/json"))
{
string responseContent = await responseMessage.Content.ReadAsStringAsync();
// 处理JSON格式数据
}
else if (responseContentType.Equals("text/xml"))
{
string responseContent = await responseMessage.Content.ReadAsStringAsync();
// 处理XML格式数据
}
}
下面是两条示例说明:
示例一:HTTP GET请求
以下示例代码演示如何使用HttpClient向指定URL发送HTTP GET请求,并处理响应内容。
using System.Net.Http;
async Task<string> GetHttpResponse(string url)
{
using (HttpClient httpClient = new HttpClient())
{
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, url);
requestMessage.Headers.Add("Accept", "application/json");
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
if (responseMessage.IsSuccessStatusCode)
{
string responseContentType = responseMessage.Content.Headers.ContentType.MediaType;
if (responseContentType.Equals("application/json"))
{
string responseContent = await responseMessage.Content.ReadAsStringAsync();
// 处理JSON格式数据
return responseContent;
}
else if (responseContentType.Equals("text/xml"))
{
string responseContent = await responseMessage.Content.ReadAsStringAsync();
// 处理XML格式数据
return responseContent;
}
}
return string.Empty;
}
}
示例二:HTTP POST请求
以下示例代码演示如何使用HttpClient向指定URL发送HTTP POST请求,并处理响应内容。
using System.Net.Http;
async Task<string> PostHttpResponse(string url, string requestBodyJson)
{
using (HttpClient httpClient = new HttpClient())
{
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, url);
requestMessage.Content = new StringContent(requestBodyJson, Encoding.UTF8, "application/json");
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
if (responseMessage.IsSuccessStatusCode)
{
string responseContentType = responseMessage.Content.Headers.ContentType.MediaType;
if (responseContentType.Equals("application/json"))
{
string responseContent = await responseMessage.Content.ReadAsStringAsync();
// 处理JSON格式数据
return responseContent;
}
else if (responseContentType.Equals("text/xml"))
{
string responseContent = await responseMessage.Content.ReadAsStringAsync();
// 处理XML格式数据
return responseContent;
}
}
return string.Empty;
}
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:c#发送请求访问外部接口的实例 - Python技术站