将HTTP请求发送到第三方服务器并获得重定向响应

post http request to 3rd party server and get response with redirect

本文关键字:重定向 响应 服务器 第三方 请求 HTTP      更新时间:2023-09-26

我有asp.net mvc项目的形式,我需要发送作为httpRequestObject。我已经尝试了几天,让简单的XMLhttp请求第三方信用卡清算公司的url,并得到重定向的XML格式的响应-我不在乎是否重定向由iframe或弹出在互联网上检查了解决方案,在JavaScript上尝试了它-但据我所知,我无法用JS做到这一点,也尝试了asp.net和c#,但对我来说没有任何作用。检查了这里所有的解决方案,但仍然没有工作。我检查了我是否被阻止了,比如代理或防火墙,这不是问题。

我当前的JS代码是-

function createMPITransaction() {
var terminal_id = "0962832";
var merchant_id = "938";
var user = "my-user";
var password = "my-password";
var url="https://cguat2.creditguard.co.il/xpo/Relay";
var xmlStr = "my string";

var http = new XMLHttpRequest();
http.open("POST",url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader('Access-Control-Allow-Headers', '*');
http.setRequestHeader('withCredentials', true);
http.setRequestHeader('responseType', 'text');
var response = http.responseText;

http.onreadystatechange = function () {//Call a function when the state changes.
    if (http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
console.log(xmlStr);
console.log(http);
http.send(xmlStr);

并从控制台获取-

 XMLHttpRequest {readyState: 1, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, responseURL: ""…}

我能在JS上做吗?如果没有,我如何在asp.net c#上做到这一点?限制对第三方服务器的请求,并获得重定向是不常见的,使其成为真正的挑战。

就重定向代码而言,您可以看到类似的答案,例如:https://stackoverflow.com/a/3836811/6298965

你可能仍然错过的是检查你的请求是否符合规范,或者你实际上得到了一个错误,所以你没有重定向。

经过初步分析后,我猜api调用可能需要jsonxml。

此外,如果您使用或至少查看github实现会更好:https://github.com/mderazon/creditguard-node/blob/master/lib/creditguard.js