c# using TinyUrl Api(getting original address from shortened address) - c#

i found the tinyurl api to shorten the Url. you can see from the link below.
https://blogs.msdn.microsoft.com/bramveen/2009/01/06/converting-url-to-tinyurl-in-c/
And I also want to get original address from my shortened url.
but i can't find that reverse api.
anyone knows how to reverse it?

Looking at the api there is no api to get the reverse. But all tinyurl is doing when you send a HTTP request to tiny url is return a HTTP 301 with the original url in the Location response header. So you could do something like this.
// Creates an HttpWebRequest for the specified URL.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// Sends the HttpWebRequest and waits for response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Now you can use the response object to read the location header value (which is your original url).

Related

Get JSON string from HTTP GET request

Using fiddler to observe this URL:
http://opencaselist.paperlessdebate.com/bin/AllDocs?view=attachments#format=json?|t=allattachments&p=1&l=10&s=filename&d=asc
I find a nice JSON response like this
How do I get this response into a string that I can save in a txt file using C#? Is there a way to turn an HTTP Web Response to a string? Is there something is NewtonSoft JSON that can help me? Are there particular terms that will help me google this more effectively?
Every time I try I just get an HTML version of the web-page at the link and not the JSON data I'm trying to get:
string url = "http://opencaselist.paperlessdebate.com/bin/AllDocs?view=attachments#|t=allattachments&p=1&l=10&s=filename&d=asc";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = WebRequestMethods.Http.Get;
httpWebRequest.Accept = "text/json";
httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
response.Close();
Note that on that screenshot, there are some differences:
the URL is way different: AllAttachements vs AllDocs, but that's minor, I'm pointing it out "just in case"
the PARAMS are way different: the screenshot specifies xpage=plain&outputSyntax=plain and your code - not
the HEADERS are different: your code has Accept=text\json while screenshot has Accept: text/javascript
Have you tried using the same params and headers?
EDIT: also, I've opened up the page from your code, and it actually is a page. After loading, it generates additional requests to
http://opencaselist.paperlessdebate.com/bin/get/XWiki/AllAttachmentsResults?xpage=plain&outputSyntax=plain&offset=1&limit=10&reqNo=1&sort=filename&dir=asc
which, if you download, results in JSON data. No headers at all, simple GET. I've just got the JSON data by simply pasting that URL into Chrome.. I think that you simply use wrong URL.

Get query string from a http response

If I copy and past the following URL in the browser, I get a response URL with a query string sessionid in it:
https://abc.abcdefg.com/abcd/sessionServlet
I am trying to capture that response URL and session id in my code behind in .net:
string url = "https://abc.abcdefg.com/abcd/sessionServlet";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Label1.Text = response.ResponseUri.ToString();
the response.ResponseUri contains my original URL, but not the response URL I get back from the sessionServlet.
Could anyone help me? Thank you in advance.
Looking at your comment about the http://devserver/myproject/login.aspx?sessionid=1341351j1oij4o1i3o13i5ho1i3j4134o URL appearing the browser from vising the URL https://abc.abcdefg.com/abcd/sessionServlet, you're probably being redirected using HTTP 301 or 302.
If so, I'd add request.AllowAutoRedirect = true MSDN which will allow your web request to follow that redirect. Then response.ResponseUri.Query should have the querystring that you're looking for.

How can I know from the URL if it is the correct image URL or not in C#?

I have an array of images URLs. I want to know which of these URLs are correct and which not, without using try-catch, and I want to do that as fast as possible.
I would think the only way for you to know which urls are correct is to just make an HTTP request to the URL. If you have a lot of pictures, this will always take time. You can minimize that time by just making a HEAD HTTP request (as opposed to a GET and downloading the whole response), and checking the status code of the response. If the status code is 200, you might assume that you get the picture you're looking for, if it is 404, you know the url is incorrect.
Code might be something like:
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://example.com/");
req.Method = "HEAD";
HttpWebResponse resp = (HttpWebResponse)(req.GetResponse());
HttpStatusCode statuscode = resp.StatusCode;
A note on getting a 200 response: If you get a 200 repsonse, you cannot be sure that you are actually getting image you want. You might be getting something else, e.g. a redirect from the image url.

WebRequest being decoded?

I have a string that stores a URL that contains %26 for & at one point in the URL, because that is required for the URL that I am trying to call.
I print the URL string to the console before making the
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(myURL);
and if I copy and paste the URL that is printed, then I am able to get to the desired target.
However, when I connect and get the response with
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
it fails (the URL makes an update to another page, and the update is made when I copy and paste the URL that is printed, but not when I run the HttpWebResponse...).
Does this have something to do with the encoding/decoding of the URL string when the WebRequest is created?
Any help would be appreciated, thanks.
Try encoding your url. Try this link URL Encode

Determine Final Destination of a Shortened URL

I'm trying to find the best way (in code) to determine the final destination of a shortened URL. For instance http://tinyurl.com redirects to an eBay auction. I'm trying to get the URL for the eBay auction. I'm trying to do this from within .NET so I can compare multiple URLs to ensure that there is no duplicates.
TIA
While I spent a minute writing the code to ensure that it worked the answer was already delivered, but I post the code anyway:
private static string GetRealUrl(string url)
{
WebRequest request = WebRequest.Create(url);
request.Method = WebRequestMethods.Http.Head;
WebResponse response = request.GetResponse();
return response.ResponseUri.ToString();
}
This will work as long as the short url service does a regular redirect.
You should issue a HEAD request to the url using a HttpWebRequest instance. In the returned HttpWebResponse, check the ResponseUri.
Just make sure the AllowAutoRedirect is set to true on the HttpWebRequest instance (it is true by default).
One way would be to read the URL and get the result code from it. If it's a 301 (permanent redirect) then follow where it's taking you. Continue to do this until you reach a 200 (OK). When using tinyurl it could happen that you will go through several 301 until you reach a 200.
Assuming you don't want to actually follow the link, for TinyURL you can append /info to the end of the url:
http://tinyurl.com/unicycles/info
and it gives you a page showing where that tinyurl links to, which I assume would be easy to parse using xpath or similar.
Most other URL shortening services have similar features, but they all work differently.

Categories