0

I am using HttpClient PostAsync to send data to a URI. However, the following code doesn't behave as expected:

        using (var client = new HttpClient())
            {
                var values = new Dictionary<string, string>
                {
                    {"cpm_site_id",TOKEN},
                    {"apikey",API_KEY},
                    {"cpm_amount",input.Amount},
                    {"cpm_currency",input.Currency},
                    {"cpm_trans_id",input.Id},
                    {"cpm_custom",input.Custom},
                };

                // Get the parameters in the url encoded format
                var content = new FormUrlEncodedContent(values);

                //Send request
                var response = await client.PostAsync(new Uri(Urls.GetUrl(Methods.Pay, IS_PRODUCTION_SITE)), content);

When the client closes their browser, I want to receive an event notification to call this code, send the above data to the client, and open a new browser instance to perform additional actions. However, this code doesn't accomplish this and I'm not sure exactly why.

Jmocke
  • 269
  • 1
  • 10
  • 20
  • I'm confused about what you're asking; you use HttpClient to post information to a *server*, not take some action on the client side. The code you've posted has nothing to do with what you say you're trying to do. – EJoshuaS - Stand with Ukraine Aug 23 '16 at 16:52
  • @EJoshuaS am i using the wrong approach to perfom what i want to achieve ? what is the correct way of doing that ? – Jmocke Aug 23 '16 at 16:54
  • Can you give more context for the code in question? Where is this code? Who are you trying to call? Is this some kind of client-server application or does this reside in a RESTful service or ASP.NET web site? – EJoshuaS - Stand with Ukraine Aug 23 '16 at 16:55
  • @EJoshuaS I am trying to send the data to a uri – Jmocke Aug 23 '16 at 16:57
  • Yes, I can see that, but it's hard to understand what you're trying to accomplish without more context information. What kind of application is the code in question a part of? – EJoshuaS - Stand with Ukraine Aug 23 '16 at 16:59
  • I am using visual studio web forms, i dont know if i answer your question .. – Jmocke Aug 23 '16 at 17:03
  • Yes, that does actually - technically it isn't "Visual Studio Web Forms" it's "ASP.NET Web Forms." So this code resides on the server, the client posts data to the server, and you want to return a value to the client and have them open a new browser with it? Or do you want this to be in an event handler and have the server receive event notifications of an event on the client side? – EJoshuaS - Stand with Ukraine Aug 23 '16 at 17:07
  • To make it clear to You, i want to send client request to a new uri open in the new browser and wait for browser close event to continue the task. this is the same scenario the way paypal button work. when you are on a e-commerce site , you want to pay with paypal you click on the pay button and this open the paypal page for you to process. – Jmocke Aug 23 '16 at 17:13

1 Answers1

0

I think you'll need to use something like Selenium to automate a web browser. The HttpClient can perform HTTP functions, but does not work like a web browser does.

See this SO post for a 'hello world' example

See this SO post for an example of capturing the browser close event. I've not done this with C#, but I'd imagine it'll be similar to this JAVA example.

Community
  • 1
  • 1
yanigisawa
  • 731
  • 1
  • 8
  • 19