繁体   English   中英

如何在 ASCX 页面上的 ASP 网站上从 C# 代码执行 url 调用?

[英]How can I execute an url call from C# code on a ASP website on a ASCX page?

我需要调用在客户端上注册的自定义协议(类似于:“custom:signDocs?param1=value?param2=value”)。

我有一个通过 JavaScript 单击按钮执行的工作。

但我需要调用 url 来执行我在客户端电脑上的程序。

该程序用于签署文档并将它们发送回服务器,并且在代码中我有一个 15 分钟的计时器,它等待文档的状态更改为已签名,然后将文档显示给用户。

我也尝试使用 webrequest:

    //Method that uses the webrequest
    {
        System.Net.WebRequest.RegisterPrefix("customProtocolName", new PrototipoIDPTRequestCreator());
        System.Net.WebRequest req = System.Net.WebRequest.Create(protocolUrlWithParams);
        var aux = req.GetResponse();
    }

    internal class CustomRequestCreator : System.Net.IWebRequestCreate
    {
        public WebRequest Create(Uri uri)
        {
            return new CustomWebRequest(uri);
        }
    }

    class CustomWebRequest: WebRequest
    {
        public override Uri RequestUri { get; }
        public CustomWebRequest(Uri uri)
        {
            RequestUri = uri;
        }
    }

但这无济于事,我不知道它甚至是正确的道路......

有谁知道实现这一目标的方法?

您可以使用System.Net.Http中的 HttpClient,如下例所示。

简单地从测试 api 端点获取呼叫。

 using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("YOUR_BASE_URL"); //https://localhost:8000
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = await client.GetAsync("api/test"); //api uri
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
            }

注意:更多详细信息,请参阅HttpClient Doc

暂无
暂无

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

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