Issues retrieving facebook social plugin comments for page, C# HttpWebRequest class - c#

I'm hoping I've done something knuckle-headed here and there is an easy answer. I'm simply trying to retrieve the list of comments for a page on my site. I use the social plug-in and then retrieve the comment id via the edge event. Server side I send the page id back and do a simple request using a HttpWebRequest. Worked well back in October, but now I get an 'internal error' response from FB. I can use the same url string put it into a browser and get the comments back in the browser in json.
StringBuilder url = new StringBuilder();
url.Append("https://graph.facebook.com/comments/?ids=" + comment.page);
string requestString = url.ToString();
HttpWebRequest request = WebRequest.Create(requestString) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Ideas? Thanks much in advance.

Since you're using the Facebook C# SDK (per your tag), try:
var url = "{your url}";
var api = new Facebook.FacebookClient(appId,appSec);
dynamic commentsObj = api.Get("/comments/?ids=" + url);
dynamic arrayOfComments = commentsObj[url].data

Related

Getting httpresponse from other site

Need help. I want to get the returned data from this link - http://www.pse.com.ph/stockMarket/companyInfoSecurityProfile.html?method=getListedRecords&common=yes&ajax=true
However, if you copy and paste that link to your browser you get Access Denied ( See Tab Title). But if you paste this link first http://www.pse.com.ph ( load the page) then paste again the link above you data.
Here is my code. I am using RestSharp
string url = "http://www.pse.com.ph/stockMarket/companyInfoSecurityProfile.html?method=getListedRecords&common=yes&ajax=true";
var client = new RestClient();
client.BaseUrl = new Uri(url);
var request = new RestRequest();
IRestResponse response = client.Execute(request);
var strResult = response.Content;
return Ok("OK");
It takes so much time getting the response from the site. Maybe because of the source site behavior?
Thank you so much
I think it should be the response of your site.
Try testing another way around.
Maybe due to the slow response, your host prevent the request.

Post Not Publishing to Page via FaceBook API

All I am trying to do is post to a page using the API. This task was extremely simple using Twitter; but with FaceBook, it has been very challenging.
I am using the following code:
string url = #"https://graph.facebook.com/{page_id}/feed?message=Hello&access_token={app_id}|{app_secret}";
WebClient client = new WebClient();
Stream data = client.OpenRead(url);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
Console.WriteLine(s);
It returns data like this:
{"data":[{"story":"Page updated their cover photo.","created_time":"2017-03-13T22:49:56+0000","id":"1646548358..._164741855..."}...
But, the post is never seen on the page! How can I successfully post from my app to my page?
Your request should be a POST request as the data you're getting back suggests this is a GET request.
Also you need the publish_pages permission to successfully post to a page

Login to website using C#

Before everyone gets upset that this has been answered. I have scoured the web looking for how to do this and have tried a number of methods. Login to website, via C# and How to programmatically log in to a website to screenscape? Both of these were helpful but I cannot figure out why I cannot get past the login page. Here is my code:
string url = "https://www.advocare.com/login.aspx";
string url2 = "https://url.after.login";
HttpWebRequest wReq = WebRequest.Create(url) as HttpWebRequest;
wReq.KeepAlive = true;
wReq.Method = "POST";
wReq.AllowAutoRedirect = false;
wReq.ContentType = "application/x-www-form-urlencoded";
string postData = "ctl00$cphContent$txtUserName=Username&ctl00$cphContent$txtPassword=Password";
byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
wReq.ContentLength = dataBytes.Length;
using (Stream postStream = wReq.GetRequestStream())
{
postStream.Write(dataBytes, 0, dataBytes.Length);
}
HttpWebResponse wResp = wReq.GetResponse() as HttpWebResponse;
string pageSource;
wReq = WebRequest.Create(url2) as HttpWebRequest;
wReq.CookieContainer = new CookieContainer();
wReq.CookieContainer.Add(wResp.Cookies);
HttpWebResponse wResp2 = wReq.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(wResp2.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
Everytime I look at pageSource it is the HTML for the login.aspx page. I must be missing something here. Maybe it's not taking the cookie, I don't know. One question I have aside from, why doesn't this work, is in the string postData = "". Are those suppose to be the name or id portion of the html tag? Any help on this is greatly appreciated as I am stumped and will have to find a different way. I would like to continue with the WebRequest and WebResponse instead of using WebBrowser. If I can't, oh well. Thanks again for any help!
What are you trying to do besides login? If its like QAing a site programically, i would suggest using selenium andcreate a c# app based off of that. If u want i can post a link to a base project for a selenium based project.
Don't necessarily view the page source, but look at the actual HTTP POST. Install a HTTP proxy such as Fiddler and then re-visit the page you are trying to emulate. Complete the HTTP POST request, and check out the results produced in the proxy. From there you'll be able to see the actual parameters, cookies, headers, etc. that are being passed and you can then attempt to replicate this in your code. It's often easy to miss something when simply viewing the HTML source but monitoring the network traffic is pretty straight forward.

URI Update Request

From my C# console application, I want to issue an Uri update request. Like the following:
http://username:password#dynupdate.no-ip.com/nic/update?hostname=mytest.testdomain.com&myip=1.2.3.4
I have tried the following:
string url = "http://username:password#dynupdate.no-ip.com/nic/update? hostname=mytest.testdomain.com&myip=1.2.3.4";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 5000;
But, I am getting, Invalid URI: The format of the URI could not be determined. error.
Any idea, where I went wrong? I type the full url as shown above into a web browser and it works as expected but through the C# application, it throws an exception.
Is there any other way to implement this?
You need to create and add some credentials to the request and then access the URI without passing in the username/password.
For more information : How to: Request Data Using the WebRequest Class (Specifically the section regarding credentials)
For example;
var uri = new Uri("http://somesite.com/something");
var request = WebRequest.Create(uri) as HttpWebRequest;
request.Credentials = new NetworkCredential("myUserName","myPassword");
request.PreAuthenticate = true;
Just have look at this web page. Is this what you are referring to?
http://www.no-ip.com/integrate/request/
I think you need to use the url as
http://dynupdate.no-ip.com/nic/update
and then send the credentials as mentioned by ChrisBint. And you need to set the preference to base64 ...if there is a provision for that
+ some headers like UserAgent as mentioned in the article.
Encode the URL argument, check the below example
string url = "http://www.stackoverflow.com?question=" + HttpUtility.UrlEncode("a sentence with spaces");
WebRequest r = WebRequest.Create(url);

Using google's api to list result of search

Hey i want to list the results from a google search, i saw that you can query google's api by using urls like this one
http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Earth%Day
In the asp.net framework i have no idea how to query this url without exiting the page. I know Response.Redirect(myquery); would work but it makes the browser leave the current page, how do i get the JSON result from that query without leaving the page ?
Thanks !
ps: I did do alot of searches before asking.
Use the HttpWebRequest and HttpWebResponse classes in the .Net Framework:
var request = (HttpWebRequest)WebRequest.Create("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Earth%Day");
var response = (HttpWebResponse)request.GetResponse();
var responseText = (new StreamReader(response.GetResponseStream())).ReadToEnd();
http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

Categories