简体   繁体   English

如何设置HttpClient PostAsync来调用新的Web浏览器

[英]How do I set up HttpClient PostAsync to call a new web browser

I am using HttpClient PostAsync to send data to a URI. 我正在使用HttpClient PostAsync将数据发送到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. 但是,此代码无法完成此操作,我不确定原因为何。

I think you'll need to use something like Selenium to automate a web browser. 我认为您需要使用Selenium之类的工具来自动化Web浏览器。 The HttpClient can perform HTTP functions, but does not work like a web browser does. HttpClient可以执行HTTP功能,但不能像Web浏览器那样工作。

See this SO post for a 'hello world' example 请参阅此SO帖子中的“ hello world”示例

See this SO post for an example of capturing the browser close event . 有关捕获浏览器关闭事件的示例,请参见此SO帖子 I've not done this with C#, but I'd imagine it'll be similar to this JAVA example. 我没有使用C#完成此操作,但是我想它会与此Java示例相似。

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

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