WebClient DownloadString not getting all the information - c#

I'm trying to do the following:
string url = #"https://picasaweb.google.com/data/feed/api/user/bladibla?thumbsize=206c";
WebClient myClient = new WebClient();
string picasaxml = myClient.DownloadString(url);
When I go to the url (which is not the real url ofcourse, the "bladibla" is not the actual username), I get to see all the information.
When I look at the picasaxml, I'm missing parts of the information. Some xml-sections are missing in the document.
Can anyone help me?
-- Update --
The real url is : https://picasaweb.google.com/data/feed/api/user/tim#boerenbond.be?thumbsize=206c
so if you go to that url, you should get a lot of information, including the names of some photoalbums that were created there.
But when I run the code as displayed higher, i'm not getting all that information.
Ok, I just noticed that when I go to the page on another machine, I'm not getting all the info either.

Related

How to read URL starting with view-source in C#

I want to read following URL and it should save the content available in the page to Text file.
I use below code to read page source:
string address = "view-source:http://stackoverflow.com/"; //any web site url
using (WebClient wc = new WebClient())
{
var Text= wc.DownloadString(address);
}
But it is throwing exception "The URI prefix is not recognized."
Any Help Would be Appreciate.
Thanks! in advance.
You're using a feature of Chrome by prepending "view-source:" to that url. The WebClient class probably doesn't know anything about that feature. It's complaining about the "URI prefix" being unrecognized. That's the "view-source:" portion of your string.
So, remove that part of the URL and you will have a valid url.
string userInput = "view-source:http://stackoverflow.com/";
string address = userInput.Replace("view-source:", "");
Note: this may produce different results for web apps that provide additional content after javascript has been run and interpreted. You might not ultimately get what you want.
Edit: after your comment, it sounds like you want to remove the possibility of the url starting with "view-source:" which I have reflected in the answer.
Just in case you're looking for the "post javascript" source. There's a project on github that offers this feature but I've never used it. I only know about it because it's maintained by a guy I work with.
You can also find a working example in this repl

Error using DropNet

I have the following error when using DropNet library to make connection to DropBox.
Trying to implement it for my CMS and also for my knowledge :)
trying the current documentation of DropNet.
Source:https://github.com/DropNet/DropNet
Current code:
var _client = new DropNetClient(Core.DropBoxAPI, Core.DropBoxKeySecret);
UserLogin login = _client.GetToken();
_client.UserLogin = login;
String Url = _client.BuildAuthorizeUrl();
Produces:
Received Response [Unauthorized] : Expected to see [OK]. The HTTP
response was [{"error": "Unauthorized"}].
Exception: DropNet.Exceptions.DropboxRestException: Received Response
[Unauthorized] : Expected to see [OK]. The HTTP response was
[{"error": "Unauthorized"}].
I want to let the user connect to the Url returned by _client.BuildAuthorizeUrl(); but it even won't let me get to url
and i also have a Key and Secret generated from DropBox.com
if i try to set the method _client.BuildAuthorizeUrl(); as first statement it gives also an error on the DropBox page itselfs.
I tried to find an another post on Stackoverflow but i didn't find any solution so fare.
Its still using the basics of the documentation and it goes wrong.
If you have any ideas that would be nice.
Thanks for the advices.
I was filling the secret the same as the APP key.

How can I make sure a url provided by the user is not a local path?

I'm writhing a web application (ASP.Net MVC, C#) that require the user to provide urls to RSS or Atom Feed that I then read with the following code :
var xmlRdr = XmlReader.Create(urlProvidedByUserAsString);
var syndicFeed = SyndicationFeed.Load(xmlRdr);
While debugging my application I accidentally passed /something/like/this as an url and I got an exception telling me that C:\something\like\this can't be opened.
It looks like a user could provide a local path and my application would try to read it.
How can I make this code safe? It probably is not sufficient to check for https:// or http:// at the begining of the url, since the user could still enter something like http://localhost/blah. Is there any other way, maybe with the uri class to check if an url is pointing to the web?
Edit: I think I also need to prevent the user from entering adresses that would point to other machines on my network like this example: http://192.168.0.6/ or http://AnotherMachineName/
Try:
new Uri(#"http://stackoverflow.com").IsLoopback
new Uri(#"http://localhost/").IsLoopback
new Uri(#"c:\windows\").IsLoopback

Connecting via CMIS (dotCMIS) to SP2010: exception unauthorised

Im using dotCMIS and would like to do a simple connect to my SP2010 server. Im trying to do this with C# like here http://chemistry.apache.org/dotnet/getting-started-with-dotcmis.html in the first part
So I have something like this:
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters[SessionParameter.BindingType] = BindingType.AtomPub;
parameters[SessionParameter.AtomPubUrl] = "http://mysharepoint";
parameters[SessionParameter.User] = "SPAdmin";
parameters[SessionParameter.Password] = "1234sharepoint";
SessionFactory factory = SessionFactory.NewInstance();
ISession session = factory.GetRepositories(parameters)[0].CreateSession(); //exception unathorized
But I get always the exception: DotCMIS.Expcetions.CmisRunterimException: Unathorised
Any ideas? Via browser I can login to the site with the same user/pass, so thats might be not the problem. At first I tought its because of the NTLM problem (https://issues.apache.org/jira/browse/CMIS-531) but even if Im using parameters[SessionParameter.AuthenticationProviderClass] = "DotCMIS.Binding.NtlmAuthenticationProvider"; its the same exception. And well... this exception is not really helping me. I wish I could get more information - maybe there is a better way? What else could I try? Thank you!
PS: And yes, before I started with dotCMIS I did install and configure the MS CMIS connector: http://technet.microsoft.com/en-us/library/ff934619.aspx
Your AtomPubUrl looks suspicious. I can't tell if that's a placeholder you've added to mask the real URL or if that's the actual URL you are using. If it is the actual URL it looks like it is missing the path to the AtomPub service document. To tell if that's the case, you should be able to invoke the URL, log in, and get a bunch of XML back, which is the CMIS service descriptor. If instead you are getting a user-facing page full of HTML, you are using the wrong URL.
For example, in Alfresco, users log in to /share, but the AtomPub binding is at /alfresco/cmisatom.
Yes the AtomPubUrl was wrong.
For sharepoint its not enough to post the default sp url (http://mysharepoint) or the url to the cmis lib (http://mysharepoint/cmis)
I need to point to the repository id, somehow the sp endpoint for CMIS is:
http://mysharepoint/_vti_bin/<myLib4CMIS>/<repID>?getRepositoryInfo
http://technet.microsoft.com/en-us/library/ff934619.aspx
Somehow it was confusing, but its working :) dotCMIS is really nice.

Truncating Query String & Returning Clean URL C# ASP.net

I would like to take the original URL, truncate the query string parameters, and return a cleaned up version of the URL. I would like it to occur across the whole application, so performing through the global.asax would be ideal. Also, I think a 301 redirect would be in order as well.
ie.
in: www.website.com/default.aspx?utm_source=twitter&utm_medium=social-media
out: www.website.com/default.aspx
What would be the best way to achieve this?
System.Uri is your friend here. This has many helpful utilities on it, but the one you want is GetLeftPart:
string url = "http://www.website.com/default.aspx?utm_source=twitter&utm_medium=social-media";
Uri uri = new Uri(url);
Console.WriteLine(uri.GetLeftPart(UriPartial.Path));
This gives the output: http://www.website.com/default.aspx
[The Uri class does require the protocol, http://, to be specified]
GetLeftPart basicallys says "get the left part of the uri up to and including the part I specify". This can be Scheme (just the http:// bit), Authority (the www.website.com part), Path (the /default.aspx) or Query (the querystring).
Assuming you are on an aspx web page, you can then use Response.Redirect(newUrl) to redirect the caller.
Here is a simple trick
Dim uri = New Uri(Request.Url.AbsoluteUri)
dim reqURL = uri.GetLeftPart(UriPartial.Path)
Here is a quick way of getting the root path sans the full path and query.
string path = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery,"");
This may look a little better.
string rawUrl = String.Concat(this.GetApplicationUrl(), Request.RawUrl);
if (rawUrl.Contains("/post/"))
{
bool hasQueryStrings = Request.QueryString.Keys.Count > 1;
if (hasQueryStrings)
{
Uri uri = new Uri(rawUrl);
rawUrl = uri.GetLeftPart(UriPartial.Path);
HtmlLink canonical = new HtmlLink();
canonical.Href = rawUrl;
canonical.Attributes["rel"] = "canonical";
Page.Header.Controls.Add(canonical);
}
}
Followed by a function to properly fetch the application URL.
Works perfectly.
I'm guessing that you want to do this because you want your users to see pretty looking URLs. The only way to get the client to "change" the URL in its address bar is to send it to a new location - i.e. you need to redirect them.
Are the query string parameters going to affect the output of your page? If so, you'll have to look at how to maintain state between requests (session variables, cookies, etc.) because your query string parameters will be lost as soon as you redirect to a page without them.
There are a few ways you can do this globally (in order of preference):
If you have direct control over your server environment then a configurable server module like ISAPI_ReWrite or IIS 7.0 URL Rewrite Module is a great approach.
A custom IHttpModule is a nice, reusable roll-your-own approach.
You can also do this in the global.asax as you suggest
You should only use the 301 response code if the resource has indeed moved permanently. Again, this depends on whether your application needs to use the query string parameters. If you use a permanent redirect a browser (that respects the 301 response code) will skip loading a URL like .../default.aspx?utm_source=twitter&utm_medium=social-media and load .../default.aspx - you'll never even know about the query string parameters.
Finally, you can use POST method requests. This gives you clean URLs and lets you pass parameters in, but will only work with <form> elements or requests you create using JavaScript.
Take a look at the UriBuilder class. You can create one with a url string, and the object will then parse this url and let you access just the elements you desire.
After completing whatever processing you need to do on the query string, just split the url on the question mark:
Dim _CleanUrl as String = Request.Url.AbsoluteUri.Split("?")(0)
Response.Redirect(_CleanUrl)
Granted, my solution is in VB.NET, but I'd imagine that it could be ported over pretty easily. And since we are only looking for the first element of the split, it even "fails" gracefully when there is no querystring.

Categories

Resources