[英]How do i convert the following CURL call to C#
I'm trying to use a bible API in an application. 我正在尝试在应用程序中使用圣经API。 Here is the following Curl Call which I have to make
这是我必须进行的以下卷曲调用
curl -L -u #{API Token}:X -k https://bibles.org/v2/verses/eng-GNTD:Acts.8.34.xml
Now the documentation gives us a PHP code for using this API which is as below 现在,文档为我们提供了使用此API的PHP代码,如下所示
<?php
$token = '#{API Token}';
$url = 'https://bibles.org/v2/verses/eng-GNTD:Acts.8.34.xml';
// Set up cURL
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// don't verify SSL certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// Return the contents of the response as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Set up authentication
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$token:X");
// Do the request
$response = curl_exec($ch);
curl_close($ch);
print($response);
?>
I have tried changing this in C# to the following. 我尝试将C#中的内容更改为以下内容。 However, I get a 401(Unauthorized code).
但是,我得到了401(未经授权的代码)。 What am i doing wrong?
我究竟做错了什么?
string apiToken = "#{myAPIToken}";
string url = "https://bibles.org/v2/verses/eng-GNTD:Acts.8.34.xml";
WebRequest myReq = WebRequest.Create(url);
CredentialCache mycache = new CredentialCache();
myReq.Headers["Authorization"] = Convert.ToBase64String(Encoding.ASCII.GetBytes(apiToken + ":X"));
WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
I think you are missing the Basic
portion of the authentication header. 我认为您缺少身份验证标头的
Basic
部分。 This is how I would do it in .NET 4.6 这就是我在.NET 4.6中的处理方式
var client = new HttpClient()
var auth = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{accountSid}:{token}"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", auth);
in your example try this: 在您的示例中尝试以下操作:
myReq.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(apiToken + ":X"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.