我正在尝试向 PayU 支付网关发布订单,使用诸如 postman 之类的 Rest Client 工具,我也遇到了同样的问题。
我正在尝试使用 C# 发布,订单创建成功但响应不符合预期,它应该是一个包含插入的订单 ID 和重定向 url 的 json 对象,但当前是 html 响应!
C# 代码响应:
我的 C# 代码使用 restsharp 库:
public IRestResponse<CreateOrderResponseDTO> CreateOrder(CreateOrderDTO orderToCreate)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var actionUrl = "/api/v2_1/orders/";
var client = new RestClient(_baseUrl);
var request = new RestRequest(actionUrl, Method.POST)
{
RequestFormat = DataFormat.Json
};
request.AddJsonBody(orderToCreate);
request.AddHeader("authorization", $"Bearer {_accessToken}");
request.AddHeader("Content-Type", "application/json");
var response = client.Execute<CreateOrderResponseDTO>(request);
if (response.StatusCode == HttpStatusCode.OK)
{
return response;
}
throw new Exception("order not inserted check the data.");
}
我的 C# 代码使用内置
WebRequest
也返回相同的 html : public string Test(string url, CreateOrderDTO order)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Accept = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + _accessToken);
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(new JavaScriptSerializer().Serialize(order));
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
return result;
}
}
谁能告诉我在这里错过了什么?
最佳答案
经过一些尝试,我发现 PayU rest api 返回 302
(找到)还有 ResponseUri
不是 OK 200
正如预期的那样。
默认情况下,rest 客户端会自动重定向到此 url,因此我收到了付款页面的 html 内容。
解决办法是:
client.FollowRedirects = false;
希望这对任何人有用。
关于c# - 调用PayU rest api(创建订单)返回html而不是json响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56837519/