异步HttpWebRequest问题... [英] Asynchronous HttpWebRequest Question...

查看:63
本文介绍了异步HttpWebRequest问题...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用ASP.NET Core 2几周了。到目前为止,一切都非常好。我遇到了一个并非特定于.NET核心的问题,但我想确保任何帮助都能理解我目前的情况。

I have been working with ASP.NET Core 2 for a few weeks. So far everything is really great. I've run into an issue that is not specific to the .NET core, but i wanted to ensure that any help understands my current situation.

我对我的原型有一个想法ASP.NET Core 2站点。我需要一些非常具体的Google搜索信息,这些信息仅限于我的网站...想象一下"收集信息",而不是给某人一个谷歌搜索用户界面...这改变了
信息,所以我不知道我真的想要存储它,它是被动信息(很高兴)。

I had an idea for one of my prototype ASP.NET Core 2 sites. I needed some very specific Google Search information that is exclusive to my site... Think of it like "gathering information", not giving someone a google search UI... This is changing information, so I don't really want to store it, and it is passive information (nice to have).

我安装了JSON.NET,构建了我的WebRequest,它看起来像这样;

I installed JSON.NET, built my WebRequest and it looks like this;

        public void GetGoogleData()
        {
            var url = String.Format("https://maps.googleapis.com/maps/api/place/textsearch/{0}?query={1}&type={2}&key={3}", apiFormat, apiQuery, apiType, apiKey);
            HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
            webRequest.Timeout = 20000;
            webRequest.Method = "GET";
            webRequest.BeginGetResponse(new AsyncCallback(GoogleDataCompleted), webRequest);
        }

        public void GoogleDataCompleted(IAsyncResult result)
        {
            var request = (HttpWebRequest)result.AsyncState;
            var response = (HttpWebResponse)request.EndGetResponse(result);
            using (var stream = response.GetResponseStream())
            {
                var r = new StreamReader(stream);
                var resp = r.ReadToEnd();

                SearchResults = JsonConvert.DeserializeObject<SearchResult>(resp);
            }
        }


 

这很好用......它消失了,得到我需要的数据,我需要用这个做点什么......

 

This works well... it goes out, gets the data I require and i need to do something with this...

首先,我不想要为每个页面请求点击Google API ...每天一次就足够了。

First, I do not want to hit the Google API for every page request... Once daily is sufficient.

其次,如何在控制器级别存储和检索此信息,以便将数据提供给我的控制器方法?

Second, How do I store and retrieve this information at the controller level so it is available data to my controller methods?

第三,我遇到了异步编码问题......最后一行代码是设置一个Public searchresults对象。在给定的控制器方法中,我将如何"等待"。搜索结果,然后搜索返回以前的
缓存数据,或从Google API中新获取的数据?

Third, I am having an asynchronous coding issue... The very last line of code is setting a Public searchresults object. In a given controller method, how would I "await" the result of the search, and then have the search either return the previously cached data, or the newly acquired data from the Google API?

任何帮助将不胜感激,

炖Basterash 

Stew Basterash 




推荐答案

你好,

>>我不想为每个页面请求点击Google API ...每天一次就足够了

谷歌地图api是谷歌公司为用户公开的界面,用户可以使用api获取一些有用的数据。如果你想获得一些你没有的新数据,我认为你需要调用api让你想要。

Google maps api is a interface that google company expose for the user, user can use the api to get some useful data.If you want to get some new data that you don't have , I think you need to invoke the api to get you want.

>>如何在控制器中存储和检索此信息水平,所以它是我的控制器方法的可用数据?

根据您的情况,y您可以将json数据存储到内存,文件,XML文件,SQL DB等中。至于什么是最好的方法,你应该考虑数据大小,数据持久性,存储和检索数据的性能等。如果日期不大并且没有重要,
我认为你可以创建一个成员变量来传输控制器。

For your situation,you could store json data into memory,files, XML file, SQL DB and so on. As for what is best way you should consider tha data size , data persistence, performance of store and retrieve data, etc.If the date is not larger and no important, I think you could create a member variables to transport between controllers.

>>我将如何"等待"搜索结果,然后让搜索返回先前缓存的数据

最简单的方法是 在GoogleDataCompleted 方法结束时创建成员变量并更改值。 如果 值已更改,您可以从主线程监控成员变量的值。如果更改,则会显示
表示 线程正在运行。

The easiest way is that  create a member variable and changed value at the end of GoogleDataCompleted method .  And you could monitor the value of member variable from main thread if the value has changed . If changed , it shows that  The thread is running over.

最好的问候,

feih_7

Best regards,
feih_7


这篇关于异步HttpWebRequest问题...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆