简体   繁体   中英

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

I need to call a Custom protocol (something like: "custom:signDocs?param1=value?param2=value") that is registered on a client.

I have a working one that is executed via JavaScript on a button click.

But I need to call the url to execute the program I have on the clients pc.

The program is for signing documents and sending them back to the server, and, in the code I have a 15min timer that waits for the status of the documents to change to signed then it shows the documents to the user.

I also tried using 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;
        }
    }

But this does nothing, I do not know it its even the right path...

Does anyone know of a way to accomplish this?

You can use HttpClient from System.Net.Http like the following example.

Simple get call from a test api endpoint.

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

Note: For more details refer, HttpClient Doc

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