WebRequest being decoded? - c#

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

Related

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

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).

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.

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);

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