C#,调用对方接口,POST方法,Basic 账号密码身份认证。
string url = "";
string account = "";
string pwd = "";
JObject postData = new JObject();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
//添加基础Basic认证
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri(url), "Basic", new NetworkCredential(account, pwd));
request.Credentials = credentialCache;
string code = "{" + account + "}:{" + pwd + "}";
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(code)));
byte[] byteArray = Encoding.UTF8.GetBytes(postData.ToString());
Stream requestStream = request.GetRequestStream();
requestStream.Write(byteArray, 0, byteArray.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string content = reader.ReadToEnd();
reader.Close();
标签:string,C#,Basic,request,HttpWebRequest,new
From: https://www.cnblogs.com/Nov-11/p/18066685