如何从 ASP.NET MVC 解决方案调用外部 URL

发布于 2024-09-16 16:11:22 字数 636 浏览 6 评论 0原文

第一篇文章就在这里。所以最好让它成为一个好的。

我有一个 ASP.NET MVC 2 Web 应用程序,其中有一个我需要为我进行呼叫的 actionResult。

问题是我需要这个 AR 来处理一些数据操作,之后我需要它调用一个外部 URL,这实际上是一个公司模块,用于处理向我们公司手机发送消息。

它只需要调用如下所示的 URL:

string url = "http://x.x.x.x/cgi-bin/npcgi?no=" + phoneNumber + "&msg=" + message;

我不需要任何返回消息或任何内容。只是想调用外部 URL,这当然超出了我自己的 Web 应用程序的范围。 (我不想重定向)。该 URL 必须在 GUI 后面调用,而用户却没有意识到。并且他们正在查看的页面不得受到影响。

我尝试过:

Server.Execute(url);

但是没有用。我听说有些人通过在页面上隐藏 iFrame 来解决此问题。将 src 设置为可能需要的 url,然后以某种方式执行它,以实例化调用。对我来说,这似乎不太优雅,但如果这是唯一的解决方案,是否有人有一个关于如何完成此操作的示例。或者,如果您有更时尚的建议,我会洗耳恭听。

First post inhere ever. So better make it a good one.

I have a ASP.NET MVC 2 web application in which I have an actionResult I need to do a call for me.

The thing is I need this A.R to handle some data operations and after that I need it to call an external URL which is actually a Company Module that handles sending messages to our company handset phones.

It just needs to call the URL that looks like this:

string url = "http://x.x.x.x/cgi-bin/npcgi?no=" + phoneNumber + "&msg=" + message;

I don't need any return message or anything. Just want to call that external URL which is of course outside the scope of my own web application. (I do not want to Redirect). That URL must be called behind the GUI without the user ever realising. And the page that they are viewing must not be affected.

I tried with:

Server.Execute(url);

However did not work. I've heard that some ppl go about this by having a hidden iFrame on the page. The setting the src to the url one may need and then somehow execute that, to get the call instantiated. It doesn't seem so elegant to me, but if that is the only solution, does anyone have an example as to how that is done. Or if you have a more sleek suggestion I am all ears.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

念三年u 2024-09-23 16:11:22

我终于让它与这段代码一起工作:

 string messageToCallInPatient = "The doctor is ready to see you in 5 minutes. Please wait outside room " + roomName;
 string url = "http://x.x.x.x/cgi-bin/npcgi?no=" + phoneNumber + "&msg=" +
               messageToCallInPatient;
 HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(string.Format(url));
 webReq.Method = "GET";
 HttpWebResponse webResponse = (HttpWebResponse)webReq.GetResponse();

 //I don't use the response for anything right now. But I might log the response answer later on.   
 Stream answer = webResponse.GetResponseStream();
 StreamReader _recivedAnswer = new StreamReader(answer);

I finally got it working with this piece of code:

 string messageToCallInPatient = "The doctor is ready to see you in 5 minutes. Please wait outside room " + roomName;
 string url = "http://x.x.x.x/cgi-bin/npcgi?no=" + phoneNumber + "&msg=" +
               messageToCallInPatient;
 HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(string.Format(url));
 webReq.Method = "GET";
 HttpWebResponse webResponse = (HttpWebResponse)webReq.GetResponse();

 //I don't use the response for anything right now. But I might log the response answer later on.   
 Stream answer = webResponse.GetResponseStream();
 StreamReader _recivedAnswer = new StreamReader(answer);
人生戏 2024-09-23 16:11:22

由于您不希望从 url 中返回值,最简单的方法是

  • 在 AR 执行之后,使用 Webclient 触发 URL
  • 通过 HTTP GET 或 POST(同步或异步)

示例代码

   WebClient wc = new WebClient();
   wc.UploadProgressChanged += (sender, evtarg) =>
            {
                Console.WriteLine(evtarg.ProgressPercentage);
            };

        wc.UploadDataCompleted += (sender, evtarg) =>
            {
                String nResult;
                if (evtarg.Result != null)
                {
                    nResult = Encoding.ASCII.GetString(evtarg.Result);
                    Console.WriteLine("STATUS : " + nResult);
                }
                else
                    Console.WriteLine("Unexpected Error");

            };


        String sp= "npcgi??no=" + phoneNumber + "&msg=" + message;
        System.Uri uri = new Uri("http://x.x.x.x/cgi-bin/");
        wc.UploadDataAsync(uri, System.Text.Encoding.ASCII.GetBytes(sb);

该示例使用 HTTP POST 和异步调用(因此会立即返回触发 URL 后 - 非阻塞)

Since you don't expect a return value from the url, simplest way is

  • After the AR Execution, use Webclient to trigger the URL
  • Either by HTTP GET or POST (Synchronous or Asynchronous)

Sample code

   WebClient wc = new WebClient();
   wc.UploadProgressChanged += (sender, evtarg) =>
            {
                Console.WriteLine(evtarg.ProgressPercentage);
            };

        wc.UploadDataCompleted += (sender, evtarg) =>
            {
                String nResult;
                if (evtarg.Result != null)
                {
                    nResult = Encoding.ASCII.GetString(evtarg.Result);
                    Console.WriteLine("STATUS : " + nResult);
                }
                else
                    Console.WriteLine("Unexpected Error");

            };


        String sp= "npcgi??no=" + phoneNumber + "&msg=" + message;
        System.Uri uri = new Uri("http://x.x.x.x/cgi-bin/");
        wc.UploadDataAsync(uri, System.Text.Encoding.ASCII.GetBytes(sb);

The sample uses HTTP POST and Asynchronous call( So it will return immediately after triggering the URL - Non blocking)

清泪尽 2024-09-23 16:11:22

您可以简单地使用“return Redirect(url);”

You can use simply " return Redirect(url);"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文