我现在已经被困了将近一个星期了,VISA服务有这个授权API,它使用事务令牌和会话令牌进行身份验证,出于某种原因,它会返回:
远程服务器错误(400)错误请求。
我几乎尝试过所有事情,包括:
WebClient
组件似乎没有任何效果,这就是代码:
string statusCode;
string body =
"{" +
"\"transactionToken\": \"" + transactionToken + "\"," +
"\"sessionToken\": \"" + sessionToken + "\"" +
"}";
string requestURL = "https://devapice.vnforapps.com/api.authorization/api/v1/authorization/web/{MyMerchantId}";
string id = "xxxxxx";
string password = "yyyyyyy";
string respuesta = "";
try
{
HttpWebRequest request = WebRequest.Create(requestURL) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/json";
var requestStringBytes = Encoding.UTF8.GetBytes(body);
request.GetRequestStream().Write(requestStringBytes, 0, requestStringBytes.Length);
request.Headers["Authorization"] = GetBasicAuthHeader(id, password);
request.Headers["VisaNet-Session-Key"] = sessionToken;
/*
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
var json = JsonConvert.SerializeObject(new
{
transactionToken = transactionToken,
sessionToken = sessionToken
}, Formatting.Indented);
streamWriter.Write(json);
}
*/
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
statusCode = response.StatusCode.ToString();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
respuesta = readStream.ReadToEnd();
}
}
catch (WebException ex)
{
using (WebResponse response = ex.Response)
{
var httpResponse = (HttpWebResponse)response;
using (Stream data = response.GetResponseStream())
{
StreamReader sr = new StreamReader(data);
respuesta = sr.ReadToEnd();
}
}
}
有一个先前的和第一个API为本赛季的剩余时间创建一个会话密钥,我将这个会话密钥存储在一个Session变量中,可能会发生什么?,我已经阅读了很多类似的这里的案件,并试图运用最好的,没有运气。
GetBasicAuthHeader
函数是一个自定义函数,用于建立一个适用于第一个API的基本身份验证标头。
答案 0 :(得分:0)
这可能就像回应告诉你的那样。您的当前代码无法确保在发出请求时刷新所有数据。
如果将请求流和StreamWriter
包装在using
语句中,则在处理对象时会发生刷新:
string statusCode;
string body =
"{" +
"\"transactionToken\": \"" + transactionToken + "\"," +
"\"sessionToken\": \"" + sessionToken + "\"" +
"}";
string requestURL = "https://devapice.vnforapps.com/api.authorization/api/v1/authorization/web/{MyMerchantId}";
string id = "xxxxxx";
string password = "yyyyyyy";
string respuesta = "";
try
{
HttpWebRequest request = WebRequest.Create(requestURL) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/json";
using(var rs = request.GetRequestStream()){
using(StreamWriter sw = new StreamWriter(rs)){
sw.Write(body);
}
}
request.Headers["Authorization"] = GetBasicAuthHeader(id, password);
request.Headers["VisaNet-Session-Key"] = sessionToken;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
statusCode = response.StatusCode.ToString();
using(StreamReader sr = new StreamReader(response.GetResponseStream()){
respuesta = sr.ReadToEnd();
}
}
}
catch (WebException ex)
{
using (WebResponse response = ex.Response)
{
var httpResponse = (HttpWebResponse)response;
using (Stream data = response.GetResponseStream())
{
StreamReader sr = new StreamReader(data);
respuesta = sr.ReadToEnd();
}
}
}
编辑:
如果这不起作用,请使用Fiddler检查HTTP请求,并确保它符合该服务的任何规范。