代码之家  ›  专栏  ›  技术社区  ›  Mohammad Ghanem

调用payurestapi返回html而不是json响应

  •  0
  • Mohammad Ghanem  · 技术社区  · 7 年前

    我试图发布一个订单到PayuPaymentGateway,使用Rest客户端工具,比如PostMan,我也遇到了同样的问题。

    enter image description here

    我试着用C#发布,订单创建成功,但是响应并不像预期的那样,它应该是一个json对象,包含插入的订单id和重定向url,但当前是html响应!

    代码响应: enter image description here

     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;
            }
        }
    

    有人能告诉我这里遗漏了什么吗?

    0 回复  |  直到 7 年前
        1
  •  3
  •   Mohammad Ghanem    7 年前

    经过一些尝试,我发现payurestapi返回 302 (找到)也 ResponseUri OK 200 一如预期。

    默认情况下,rest客户端会自动重定向到这个url,因此我收到了支付页面的html内容。

    解决方案是:

    client.FollowRedirects = false;
    

        2
  •  0
  •   Piotr Labunski v_d123    6 年前

    另外,我想补充一点,上面Mohammad的回答是正确的,因为我们需要将AllowAutoRedirect设置为false。我一直试图在一个控制台应用程序中实现PayU LatAM WebCheckout,我也面临着类似的问题。我从这里给出的答案中得到了一些启发: How to get Location header with WebClient after POST request

    根据答案,我写了一个示例代码:

    public class NoRedirectWebClient : WebClient
    {
        protected override WebRequest GetWebRequest(Uri address)
        {
            var temp = base.GetWebRequest(address) as HttpWebRequest;
            temp.AllowAutoRedirect = false;
            return temp;
        }
    }
    

    在创建了上面的类之后,我在Main方法中编写了以下代码:

    var request = MakeRequestLocation(new NoRedirectWebClient());
    var psi = new ProcessStartInfo("chrome.exe");
    
    psi.Arguments = request.ResponseHeaders["Location"];
    Process.Start(psi);
    

    我现在在同一个类中调用函数MakeRequestLocation。

    private static WebClient MakeRequestLocation(WebClient webClient)
    {
        var loginUrl = @"https://sandbox.checkout.payulatam.com/ppp-web-gateway-payu/";
        NameValueCollection formData = new NameValueCollection
        {
            {"ApiKey", "4Vj8eK4rloUd272L48hsrarnUA" },
            {"merchantId", "508029" },
            {"accountId", "512321" },
            {"description", "Test PAYU" },
            {"referenceCode", "SomeRandomReferenceCode" },
            {"amount", "2" },
            {"tax", "0" },
            {"taxReturnBase", "0" },
            {"currency", "USD" },
            {"signature", "Signature generated via MD5 sum" },
            {"buyerFullName", "test" },
            {"buyerEmail", "test@test.com" },
            {"responseUrl", @"http://www.test.com/response" },
            {"confirmationUrl",  @"http://www.test.com/confirmation" }
        };
        webClient.UploadValues(loginUrl, "POST", formData);
    
        return webClient;
    }
    

        3
  •  0
  •   Mateusz    6 年前

    在postman中,解决方案是关闭重定向,如下图所示:

    postman redirects turoff