.net Core 请求外网接口的HttpGet和HttpPost 公共方法的调用

    /// <summary>
    /// Get请求发送
    /// </summary>
    /// <param name="requestUrl">url地址</param>
    /// <returns></returns>
    public static async Task<string> DoGet(string requestUrl)
    {
        string responseBody = string.Empty;
        using (HttpClient httpClient = new HttpClient())
        {
            httpClient.DefaultRequestHeaders.Add("Method", "Get");
            httpClient.DefaultRequestHeaders.Add("KeepAlive", "false");
            httpClient.DefaultRequestHeaders.Add("UserAgent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");

            HttpResponseMessage response = await httpClient.GetAsync(requestUrl);
            response.EnsureSuccessStatusCode();
            responseBody = await response.Content.ReadAsStringAsync();
        }
        return responseBody;
    }
    /// <summary>
    /// post请求
    /// </summary>
    /// <param name="Url"></param>
    /// <param name="message"></param>
    /// <returns></returns>
    public static async Task<T> DoPost<T>(string Url, object message) where T : new()
    {
        var res = new T();
        string jsonContent = JsonConvert.SerializeObject(message);
        string responseBody = string.Empty;
        using (HttpClient httpClient = new HttpClient())
        {
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
            httpClient.DefaultRequestHeaders.Add("Method", "Post");
            HttpResponseMessage response = await httpClient.PostAsync(Url, content);
            response.EnsureSuccessStatusCode();
            responseBody = await response.Content.ReadAsStringAsync();
            res = (T)JsonConvert.DeserializeObject<T>(responseBody);
        }
        return res;
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是使用HttpClient5发送POST和GET请求的Java代码示例: ```java import org.apache.hc.client5.http.classic.methods.HttpGet; import org.apache.hc.client5.http.classic.methods.HttpPost; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.core5.http.ClassicHttpResponse; import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.io.entity.StringEntity; import java.io.IOException; import java.net.URI; public class HttpClientUtils { /** * 发送GET请求 * * @param url 请求的URL * @return 响应字符串 * @throws IOException */ public static String sendGetRequest(String url) throws IOException { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(url); ClassicHttpResponse response = httpClient.execute(httpGet); return response.getEntity().toString(); } } /** * 发送POST请求 * * @param url 请求的URL * @param params 请求参数(JSON格式) * @return 响应字符串 * @throws IOException */ public static String sendPostRequest(String url, String params) throws IOException { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(URI.create(url)); StringEntity requestEntity = new StringEntity(params, ContentType.APPLICATION_JSON); httpPost.setEntity(requestEntity); ClassicHttpResponse response = httpClient.execute(httpPost); return response.getEntity().toString(); } } } ``` 这里我们使用了Apache HttpClient5库来发送请求,通过封装成方法,可以方便地在其他地方调用。注意,在使用完CloseableHttpClient后,一定要关闭它,否则可能会导致连接泄露和资源浪费。在上面的示例中,我们使用了try-with-resources语法糖来自动关闭CloseableHttpClient。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦想家NO1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值