简体   繁体   English

从Windows客户端将参数传递到网页

[英]Pass parameters to web page from Windows client

I have the following method in a controller in an MVC website: 我在MVC网站的控制器中具有以下方法:

[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. 当我在地址栏中键入URL时,它工作正常,但是我需要从Windows客户端调用它。 How do I do this in C#? 如何在C#中执行此操作?

Thanks! 谢谢!

Use the WebClient class to make an HTTP request from the client. 使用WebClient类从客户端发出HTTP请求。 You'll need to add a reference to System.Web if your client project doesn't already have one. 如果您的客户端项目还没有引用,则需要添加对System.Web的引用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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