C# 代码,用于使用用户名和私钥从终结点获取数据



我有这个端点,我想从中获取数据。这个端点有一定的方法来获取数据和发布数据。现在发生的事情是,当我需要从端点获取数据时,我有用户名和私钥,我需要发布它,以便当我可以在浏览器上运行它以查看返回的数据时,它会返回一个带有哈希 ID 的 URL。下面是该网址的示例:

https://endpoint/api/product/associatedproducst?username=myusername&version=2&hash=69865ce2329d1d0904498f64d94e8392 以下是我的代码:

public string Getproduct(string endpoint, string username, string privatekey)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://endpoint");
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Headers.Add("username:" + username);
httpWebRequest.Headers.Add("privatekey:" + privatekey);
//pass basic authentication credentials
httpWebRequest.Credentials = new NetworkCredential("myuserid", "myid");
httpWebRequest.Method = "GET";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
return responseText;
}
}

我需要一些关于如何继续上面的代码的帮助和指导,以便我可以在其上生成那些 url 和哈希 id,以便我可以读取数据并显示它。

请注意,此端点具有获取某些数据的方法,我如何调用这些方法,以便正确的 url 可以从中返回以获取数据。

尝试使用 HttpClient。 我查看了 API 并提出了解决方案。 API 提到您永远不应该发送私钥。 私钥用于生成通过查询字符串发送的哈希。对于下面的私有函数,我只是猜测您的用例。

using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;

var client = new HttpClient();
public async string Getproduct(string endpoint, string username, string privateKey)
{
var queryString = string.Format("?username={0}&version=2", username).ToUpper();
var hash = GenerateHash(privateKey, queryString);
client.DefaultRequestHeaders.Accept.Clear();
//client.BaseAddress = new Uri(baseUri);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var url = string.Format("{0}{1}&hash={2}",endpoint, queryString, hash);
using (HttpResponseMessage response = await client.GetAsync(url))
{
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
return json;
}
else
{
var statusCode = response.StatusCode;
//handle code here.
}
}
}
public async void PostProduct(string endpoint, string username, string privateKey)
{
var queryString = string.Format("?username={0}&version=2", username).ToUpper();
var hash = GenerateHash(privateKey, queryString);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
var url = string.Format("{0}{1}&hash={2}", endpoint, queryString, hash);
var dict = new Dictionary<string, string>();
dict.Add("hotel_id ", "1");
dict.Add("room_id ", "2");
var content = new FormUrlEncodedContent(dict);
using (HttpResponseMessage response = await client.PostAsync(url, content))
{
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
}
else
{
var statusCode = response.StatusCode;
//handle code here.
}
}
}
private string GenerateHash(string privateKey, string queryString)
{
var md5 = MD5.Create();
string salt = queryString;
byte[] bytes = md5.ComputeHash(Encoding.ASCII.GetBytes(salt + privateKey));
return BitConverter.ToString(bytes);
}

最新更新