THE FORUM IS IN READ-ONLY MODE
How to get response of httpRequest In VB.NET When exception is thrown
-
Recently I was working on a project I have faced this problem In ob when I make httprequest it returns the status code as well as the response string also other things like headers and cookies but when I make the same request in vb.net it an exception occurred and display status code and exception string, not the response like in vb status code is 404 and response is "user not found" but in vb only exception is thrown. I have used different libraries like xnet. leaf.xnet and the OB one Extreme.net.
I want to know how to capture that response even if the exception occurs. Kindly help me I'm stuck here.
Note: I'm not making brute force attack working on a project .
-
You have to catch the HttpException, unwrap it and it will contain the response information which you can check for response code etc.
-
I didn't test it so used it as reference/idea
Leaf.xNet HttpRequest LeafRequest = new HttpRequest() { UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36", KeepAliveTimeout = 5000, //Time in Milliseconds ConnectTimeout = 5000, //Time in Milliseconds ReadWriteTimeout = 5000, //Time in Milliseconds IgnoreProtocolErrors = false, //Required for StatusCode AllowAutoRedirect = false, UseCookies = true }; var responseContent = LeafRequest.Get("http://example.com"); // ===Get Request === var statuscode = (int)responseContent.StatusCode + " " + responseContent.StatusCode.ToString(); //For Leaf.xNet LeafRequest?.Dispose(); // for leaf,xNet only Console.WriteLine (statuscode); //Expected result on sussefull --> 200 OK HttpWebRequest -->Like Ruri said try { HttpWebRequest RequestWebGet = (HttpWebRequest)WebRequest.Create("http://example.com");// ====Get Request ==== RequestWebGet.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"; RequestWebGet.Headers.Add("Pragma", "no-cache"); RequestWebGet.Headers.Add("Accept", "*/*"); RequestWebGet.Timeout = (5000); } catch (Exception ex) //for HttpWebRequest Normal No leaf.XNet { using (WebResponse response = ex.Response) { HttpWebResponse responseWebGet = (HttpWebResponse)response; var StCode = (int)responseWebGet.StatusCode + responseWebGet.StatusCode.ToString(); Console.WriteLine (StCode); //Expected example --> 304 Not Modified } }
-
@Ruri Can you please show a small piece of code to unwrap an exception?
@Br4uN I appreciate your effort but I want to capture the response not the status code like you show in leaf.xnet request.
-
Just do
responseContent.ToString()
to get the response body...
-
@Ruri
Where to place responseContent.ToString()Leaf.xNet try HttpRequest LeafRequest = new HttpRequest() { UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36", KeepAliveTimeout = 5000, //Time in Milliseconds ConnectTimeout = 5000, //Time in Milliseconds ReadWriteTimeout = 5000, //Time in Milliseconds IgnoreProtocolErrors = false, //Required for StatusCode AllowAutoRedirect = false, UseCookies = true }; var responseContent = LeafRequest.Get("http://example.com"); // ===Get Request === var statuscode = (int)responseContent.StatusCode + " " + responseContent.StatusCode.ToString(); //For Leaf.xNet LeafRequest?.Dispose(); // for leaf,xNet only Console.WriteLine (statuscode); catch ex as Httpexception End try
As in the case of exception, it directly moves the catch block, and responseContent can't be used there. Kindly guide me a little bit as I'm not good at exception handling.
-
after var statusCode etc... put var responseSource = responseContent.ToString();
mate can you code VB or are you just pasting code hoping it works? xD
-
@Ruri
I know how to code in VB but not that good as you are.var responseSource = responseContent.ToString();
The above code does give a response but if the request was successful. But in case of exception, it doesn't give any response. So is there any way to get the actual response from the server in case of exception? I have also seen the OB source but couldn't find the solution and thanks for replying.