简体   繁体   中英

Pass parameters to web page from Windows client

I have the following method in a controller in an MVC website:

[WebGet]
public void SaveInfo()
{
    string make = Request.QueryString["Make"];
    string model = Request.QueryString["Model"];

    // Do stuff....
}

It works fine when I type the URL in the address bar, but what I need to do is call this from a Windows client. How do I do this in C#?

Thanks!

Use the WebClient class to make an HTTP request from the client. You'll need to add a reference to System.Web if your client project doesn't already have one.

using System.Net;
using System.Web;

    static void SaveInfo(string make, string model)
    {
        using (WebClient webClient = new WebClient())
        {
            string response = webClient.DownloadString(
                String.Format(
                    "http://yoursite/ControllerName/SaveInfo?make={0}&model={1}",
                    HttpUtility.UrlEncode(make),
                    HttpUtility.UrlEncode(model)
                )
            );
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM