[英]C# Passing a byte array as a Get Parameter to a web service using HttpRequestMessage
我需要致电第三方公司的网络服务来授权我的访问。 流程以这种方式工作。 第三方应用程序调用我的api控制器操作,并传递一个表示Guid(客户端ID)的字符串参数(令牌)。 之后,我需要致电他们的其他服务,以检查接收到的“令牌”是否仍然有效(授权)。 问题是第二个调用要求我将GUId作为字节数组发送。 我做了很多尝试,但不知道如何将转换为字节数组的Guid作为Get参数发送。 这是我的代码。 这将返回错误请求。
private async Task<User> VerifyTokenAsync(string token)
{
try
{
byte[] bytesToken = new Guid(token).ToByteArray();
string serviceUrl = string.Format("http://third.party.RequestTokenAuthorization?token={0}", bytesToken);
using (HttpClient client = new HttpClient())
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(serviceUrl)))
using (HttpResponseMessage response = await client.SendAsync(request))
{
if (response.StatusCode == HttpStatusCode.OK)
{
var stream = await response.Content.ReadAsStreamAsync();
}
else
{
//todo
}
}
}
catch (Exception ex)
{
Log.SaveError(ex);
return null;
}
}
添加字节数组后的URL如下所示:http://third.party/RequestTokenAuthorization?token=System.Byte []
Obs:我将服务作为ServiceReference添加到了我的项目中,并使用客户端生成的方法对其进行了调用:
byte[] bytes = new Guid(token).ToByteArray();
ServiceReference1.Client client = new ServiceReference1.Client();
var response = client.RequestTokenAuthorization(bytes);
我不确定,但是由于我正在使用asp.net网络api,因此向我的项目添加服务引用对我来说似乎是“错误的”。
谢谢!
好的,您的问题很容易看到:
token=System.Byte[]
当您在字节数组上调用ToString()时,就会发生这种情况,这就是String.Format的作用。 由于我不知道您的输入字符串显示什么,因此我将按此顺序一次尝试以下选项:
new Guid(token).ToString("N")
; Convert.ToBase64(bytesToken)
为您的URL创建一个Base-64字符串。 尝试并请返回结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.