Get query string from a http response - c#

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.

Related

c# httpwebrequest follow redirect

Httpwebrequest doesn't seem to be following the auto redirect from request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Https://www.example.com);
request.AllowAutoRedirect = true;
which redirects to
https://www.example.com/pageunknown
however
HttpWebResponse response = (HttpWebResponse)request.GetResponse()
is reading the contents from
Https://www.example.com
and not the redirects content
https://www.example.com/pageunknown
how can I get it so that the redirection is followed and httpwebresponse reads from the redirection page and not the original url/request?

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.

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

HttpWebRequest returns empty text

When I try to fetch the content of this URL
http://www.yellowpages.com.au/qld/gatton/a-12000029-listing.html
using System.Net;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
request.AllowAutoRedirect = true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader objSR;
objSR = new StreamReader(resStream, System.Text.Encoding.GetEncoding("utf-8"));
string sResponse = objSR.ReadToEnd();
I don't get any response from the server. Please help me find out why this happens.
Thanks in advance!
It may well be looking at the user agent and refusing to serve content to a client that doesn't identify itself. Try setting the UserAgent property on your request object.
Looks to me like that site is checking the referrer url and may be serving up empty content if an invalid referrer is specified.
Try setting request.Referer = "http://www.google.com";. Experiment with the referrer to see if that changes the response. I'd also try the UserAgent property as Matthew suggested.
I had the same problem and the cause was that I previously had set the method to HEAD and in later revisions had the need to parse the body.

Categories