gpt4 book ai didi

c# - 如何在请求后捕获 REST API 发送的响应 token ?

转载 作者:太空狗 更新时间:2023-10-29 17:50:34 24 4
gpt4 key购买 nike

我正在使用 REST API,我正在使用基本身份验证,其中密码按如下方式编码为 Base64

 private XmlDocument sendXMLRequest(string requestXml)
{
string destinationUrl = "https://serviceapi.testgroup.com/testtp/query";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("API_TEST_NR:Testnol1$"));
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
request.Method = "POST";

request.ContentLength = bytes.Length;
//request.Connection = "keep-alive";
request.ContentType = "text/xml";
request.KeepAlive = true;
request.Timeout = 2000;
request.MediaType = "text/xml";
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response;
Stream responseStream;

using (response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
{
responseStream = response.GetResponseStream();
XmlReader reader = new XmlTextReader(responseStream);

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(reader);


try { reader.Close(); }
catch { }
try { responseStream.Close(); }
catch { }
try { response.Close(); }
catch { }
return xmlDoc;
}
}
try { response.Close(); }
catch { }
return null;
}

我是 Web Api 的新手,我知道 API 在成功授权后基于 API 文档响应访问 x-token,我不确定如何从 HTTP header 访问或捕获它.

我可以知道实现此目标的好方法吗?

最佳答案

这比我想象的只用它的名字捕捉要容易。

string xtoken= response.Headers["custom-header"];
Console.WriteLine(xtoken);

关于c# - 如何在请求后捕获 REST API 发送的响应 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31165548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com