Getting httpresponse from other site - c#

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.

Related

REST request getting blocked?

Using RestRequest to a REST endpoint (Asana). Some machines get the request returned properly and some don't get anything. Doing more troubleshooting I think that the machines that do not get responses have IE as their default browser.
If I test the url manually in a Chrome browser I get the response. If I test in the IE browser then I get a "Do you want to Open or Save" prompt. I=My theory is that an IE setting of somekind is blocking the request with this question and since it is an app it just blocks.
My code is simple:
var client = new RestClient(#"https://app.asana.com/api/1.0/workspaces/99999999999999/tasks/search);
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Bearer " + asanaToken);
IRestResponse response = client.Execute(request);
How can I get around this?

Can not Access a URL using Webrequest

I am trying to get access a URL using .Net but when I run my Program I get the Error The remote server returned an error: (403) Forbidden. Now, the issue is if I click the link http://thisIsMyUR, and enter the user name and password as in the below code. It totally works. I am not able to understand why this exception is coming? Please refer the code below.
Side Note: I am using this sample function below to fire Build of my project in Jenkins Server.
string url = "http://thisIsMyURL";
WebRequest webRequest = WebRequest.Create(url);
webRequest.Credentials = new NetworkCredential("admin", "pass");
WebResponse response = webRequest.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseText = reader.ReadToEnd();
Other than what was recommended by Bradley Uffner in the comments, you can try to provide an actual user agent during the request. I have seen certain servers which would not respond to requests without that header for some odd perceived security reason.
EDIT: as requested, i'll update with some more information.
Some servers may choose to ignore requests, giving some error code (or closing connection) when certain conditions are not met. A good way of checking if that is the case, is to actually send all the standard headers sent by your average web browser in the request. The "User agent" is one of those headers, and this example adds it to your request:
string url = "http://thisIsMyURL";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.UserAgent = "USER AGENT VALUE";
HttpWebRequest.Credentials = new NetworkCredential("admin", "pass");
WebResponse response = webRequest.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseText = reader.ReadToEnd();
The cast to HttpWebRequest is required so you have access to the UserAgent header field. Other standard HTTP fields can be accessed this way.
You can check which headers are sent by your browser of choice by inspecting its requests using the browser developer tools (usually Right click on webpage -> Inspect elements -> Network).
I figured it out with the help of some articles. A basic authentication needs to be added. Below is a sample.
var userName = "admin;"
var password = "password";
var encodedAuthentication = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(userName + ":" + password));
webRequest.Headers.Add("Authorization", "Basic " + encodedAuthentication);

Google Map Api to Find Only Address Name And Postal Code Of all Shops in City

I am trying to fetch Address Name and Postal Code info of All Shops in specific City.e.g Amsterdam.
I want to do as same done on this website.
https://www.openingstijden.nl/Noord-Holland/Amsterdam/Drukkerijen/
But i am creating a desktop app.
I was trying this Url
https://maps.googleapis.com/maps/api/js?sensor=false&language=nl&key=MY_API_KEY&callback=gmap_draw to fetch but i ain't much deeper in javascript.
If this is correct way of doing this can any one suggest how could i fetch the same results in xml format if not then what is the correct way of fetching the same results as available on this website?
Thanks in Advance
The URL you provided is JS reference for calling the API.
string urlParameters = "";
//Your code goes here
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA");
// Add an Accept header for XML format.
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/xml"); //Keeps returning false
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
// List data response.
HttpResponseMessage response = client.GetAsync(urlParameters).Result;
//Console.WriteLine(response);
if (response.IsSuccessStatusCode)
{
// output xml
string output = response.Content.ReadAsStringAsync().Result;
//response.Content.ReadAsStringAsync();
Console.WriteLine(output);
}
You can edit the URI constructor to include parameters like address/components,key.enter code here
Please refer the documentation for more information.
https://developers.google.com/maps/documentation/geocoding/intro

Error posting a share on LinkedIn using Hammock lib

I'm trying to send a new Share on a Linkedin Person. This is my client code:
RestClient client = new RestClient()
{
Authority = "http://api.linkedin.com/v1",
Credentials = this.AccessCredentials(connectionData.ApplicationKey, connectionData.ApplicationSecret, connectionData.AccessToken, connectionData.AccessSecret),
Method = WebMethod.Post,
Encoding = Encoding.UTF8,
};
RestRequest request = new RestRequest()
{
Path = "people/~/shares",
Encoding = Encoding.UTF8,
};
Share share = new Share(socialMessage.Text, socialMessage.Name, socialMessage.Description, VisibilityCode.Anyone);
share.Content.SubmittedImageUrl = socialMessage.PictureLink;
share.Content.SubmittedUrl = socialMessage.Link;
String content = Utilities.SerializeToXml<Share>(share);
client.AddPostContent(System.Text.Encoding.UTF8.GetBytes(content));
client.AddHeader("Content-Type", "text/xml");
request.AddPostContent(System.Text.Encoding.UTF8.GetBytes(content));
request.AddHeader("Content-Type", "text/xml");
RestResponse response = client.Request(request);
I always obtain this error message after the call "Couldn't parse share document: error: Unexpected end of file after null".
Does anyone can tell me how to use Hammock library to send a POST to LinkedIn?
Thanks & Regards
Also there is possible solution here:
https://github.com/danielcrenna/hammock/issues/4
I'm not sure how to use the hammock library, but you can debug API calls for LinkedIn (or any other web service) using the tips at
http://developer.linkedin.com/documents/debugging-api-calls
This will show you how to install an HTTP sniffer and watch the traffic to see what's happening. Once you've done that, if you're still having issues post them and it'll be possible to debug what's going wrong.

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

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

Categories