首页 > 其他分享 >常见HTTP请求Web API接口

常见HTTP请求Web API接口

时间:2022-09-06 20:45:52浏览次数:104  
标签:Web jsonByteArr HTTP 请求 request 接口 API 数字证书 string

一、HTTP请求带账号和密码的域名接口

1.接口地址:http://yuming.com/test/model
2.请求头:账号:admin;密码:123456
3.接口入参格式:

点击查看代码
{
  "code": "0",
  "msg": "信息",
  "content": [
    {
      "name":"小明",
      "age":"15"
    },
    {
      "name":"小明",
      "age":"15"
    }
  ]
}
4.PostMan请求样例

a.请求头样例

b.请求报文样例

二、请求接口测试代码

点击查看代码
/// <summary>
/// Http请求封装(含账号密码的POST请求:对应PostMan 【安全认证 Basic Auth】账号密码方式请求)
/// </summary>
/// <param name="url">URL地址</param>
/// <param name="user">账号</param>
/// <param name="pwd">密码</param>
/// <param name="json">入参JSON</param>
/// <param name="contentType">请求内容类型</param>
/// <returns>请求结果</returns>
public static string PostRequestByUserPwd( string url, string user,string pwd, string json, string contentType = null)
{
    //请求接口响应结果
    string responseResultStr = "";
    try
    {
        //获取或设置用于验证服务器证书的回调
        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(DigitalCertificateCheckValidationResult);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        if (contentType == null)
        {
            request.ContentType = "application/json";
        }
        else
        {
            //请求内容类型
            request.ContentType = contentType;
        }

        //请求头内容
        string header = $"{user}:{pwd}";
        string base64Header = Convert.ToBase64String(Encoding.UTF8.GetBytes(header));
        request.Headers.Add("Authorization", "Basic" + base64Header);
        byte[] jsonByteArr = Encoding.GetEncoding("utf-8").GetBytes(json);
        request.ContentLength = jsonByteArr.Length;

        //获取请求流
        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(jsonByteArr, 0, jsonByteArr.Length);
            requestStream.Close();
        }

        //获取请求响应
        HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
        //获取请求响应流
        using (Stream responseStream = httpWebResponse.GetResponseStream())
        {
            using (StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")))
            {
                responseResultStr = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
            }
        }
    }
    catch (WebException ex)
    {
        responseResultStr = "-1";
    }
    return responseResultStr;
}

/// <summary>
/// SSL和TLS数字证书验证
/// </summary>
/// <param name="sender">发送者</param>
/// <param name="certificate">数字证书</param>
/// <param name="chain"></param>
/// <param name="sslPolicyErrors">SSL策略错误</param>
/// <returns></returns>
private static bool DigitalCertificateCheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    return true;
}
# 备注:

1.代码测试工具为:VisualStudio2019;开发工具运行环境为:Windows10
2.在请求接口需要添加请求头验证时,需要进行SSL验证。相关数字证书验证知识,可以通过搜索“SSL数字证书验证”内容进行学习。

标签:Web,jsonByteArr,HTTP,请求,request,接口,API,数字证书,string
From: https://www.cnblogs.com/Boundless-Learn/p/16654246.html

相关文章