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

时间:2022-09-29 16:56:13

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.

我有一个ASP.NET MVC 2 Web应用程序,我有一个actionResult,我需要为我做一个电话。

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.

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

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

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

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.

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

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.

但是没有奏效。我听说有些人通过在页面上隐藏iFrame来解决这个问题。将src设置为url可能需要然后以某种方式执行它,以实例化调用。它对我来说似乎并不那么优雅,但如果这是唯一的解决方案,那么有没有人会举例说明如何做到这一点。或者,如果你有一个更圆滑的建议,我全都耳朵。

3 个解决方案

#1


9  

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

#2


1  

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

由于您不希望网址返回值,因此最简单的方法是

  • After the AR Execution, use Webclient to trigger the URL
  • AR执行后,使用Webclient触发URL

  • Either by HTTP GET or POST (Synchronous or Asynchronous)
  • 通过HTTP GET或POST(同步或异步)

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)

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

#3


0  

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

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

#1


9  

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

#2


1  

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

由于您不希望网址返回值,因此最简单的方法是

  • After the AR Execution, use Webclient to trigger the URL
  • AR执行后,使用Webclient触发URL

  • Either by HTTP GET or POST (Synchronous or Asynchronous)
  • 通过HTTP GET或POST(同步或异步)

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)

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

#3


0  

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

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