webresponse from LinqToTwitter

Jun 14, 2013 at 12:31 PM
Hi Joe,

I have an application which needs to use ApplicationOnlyAuthorizer to get logged and search for some tweets.

But need just the webresponse from the api.

Before the new api version I used to perform this serach as follow:

The search engine for Twitter:
                try
                {
                    spider = Spider.RandomDelay(2, 10);
                    
                    queryResults = JsonConvert.DeserializeObject<QueryResult>(
                        spider.DownloadString(Builder.WithQuery(query).InPage(page).Build())
                    );
                    results = results.Union(Parser.Parse(queryResults)).ToList();
                }
The spider:
        public abstract string DownloadString(string url);

        protected string ReadContentFromUrl(string url)
        {
            request = (HttpWebRequest)WebRequest.Create(url);
            request.Timeout = TimeOut;

            request.UserAgent = GetUserAgent();
            var webResponse = (HttpWebResponse)request.GetResponse();
            
            var reader = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding(webResponse.CharacterSet));
            return reader.ReadToEnd();
        }
I just need the webresponse to performe my search but I'm getting some dificult to get the webresponse in the LinqToTwitter.

Could you please, help me to do this?

Thanks
Aleks
Coordinator
Jun 14, 2013 at 2:56 PM
Hi Aleks,

The code you posted doesn't contain any LINQ to Twitter, so I'm not sure what I can do to help there. Here's the documentation for LINQ to Twitter: https://linqtotwitter.codeplex.com/documentation.

@JoeMayo
Jun 14, 2013 at 4:31 PM
Thanks for your prompt help, Joe,


Really, the spider code block that I've posted contains the code version just before the api update.

Follow below the code that I'm trying to execute:
        public abstract string DownloadString(string url);

        protected string ReadContentFromUrl(string url)
        {
            var auth = new ApplicationOnlyAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
                    ConsumerSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                }
            };

            auth.Authorize();

            var twitterCtx = new TwitterContext(auth);
            var statusList = new List<Status>();

            var srch =
                (from search in twitterCtx.Search
                 where search.Type == SearchType.Search &&
                       search.Query == query
                 select search)
                .SingleOrDefault();
        }
My doubt is how can I obtain a webresponse with the results instead of the srch?
Could give me any help!

Thanks again!
Coordinator
Jun 14, 2013 at 4:53 PM
The WebResponse is hidden down inside the code. I don't expose it and it seems like doing so would interfere with the basic operation of LINQ to Twitter. However, you can get what Twitter returns by reading twitterCtx.RawResponse right after the query materializes.

@JoeMayo
Jun 18, 2013 at 6:26 PM
Hi Joe,

Sorry for delay in reponse!

Thnak you very much, the RawResukt works very well.

Follow the code solution for my spider class:
            var auth = new ApplicationOnlyAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                    ConsumerSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                }
            };

            auth.Authorize();
            //auth.Invalidate();

            var twitterCtx = new TwitterContext(auth);
            var statusList = new List<Status>();

            var srch =
                (from search in twitterCtx.Search
                 where search.Type == SearchType.Search &&
                       search.Query == query.Term &&
                       search.SearchLanguage == language &&
                       search.GeoCode == country &&
                       search.Until == date &&
                       search.Count == 50 &&
                       search.ResultType == ResultType.Mixed
                 select search.Statuses)
                .SingleOrDefault();


            var reader = new StringReader(twitterCtx.RawResult);
            return reader.ReadToEnd();
Once again, thanks for your prompt help.