zoukankan      html  css  js  c++  java
  • C# 调用api上传数据 (basic Auth )基本身份验证

    我们在这里使用的是 HttpWebRequest 进行上传的

    ///
    ///url是路径,data是json数据
    ///

    public static void upload(string url, string data, string userName, string passWord) { string Username = userName; string Password = passWord; byte[] bs = Encoding.ASCII.GetBytes(data); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "Post"; request.CookieContainer = new CookieContainer(); request.ContentType = "application/json;"; request.ContentLength = bs.Length; request.Timeout = 1000000; using (Stream reqStream = request.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); } //(1)设置请求Credentials CredentialCache credentialCache = new CredentialCache(); credentialCache.Add(new Uri(url), "Basic", new NetworkCredential(userName, passWord)); request.Credentials = credentialCache; //(2)设置Headers Authorization request.Headers.Add("Authorization", "Basic" + Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Username}:{Password}"))); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string content = reader.ReadToEnd(); } } }
  • 相关阅读:
    作业2 求题目中的数
    2013 C#单元测试
    实现项目WC
    带括号多项式版四则运算
    20道简单加减法随机生成程序扩展版体会
    20道简单加减法随机生成程序
    Jeesite 集成微信支付接口
    第一节:JAVA 语言的学习目标
    vector(未完)
    关于phpstorm端口63342的修改经历
  • 原文地址:https://www.cnblogs.com/myleave/p/14681805.html
Copyright © 2011-2022 走看看