HTTPWebrequest successfully fetched result even when wrong proxy credentials are given - vb.net

I am trying to hit a rest API using HttpWebRequest and I have to authenticate Proxy Server before I can hit a Rest API.
For authorizing proxy Server, I am adding credentials to HttpWebrequest.Proxy.credentials.
Now, my scenario is that, I am accessing the Rest API twice, first with Correct Proxy Credentials and then with wrong Proxy credentials.
With Correct Proxy Credentials I getthe out put as expected but with wrong credentials I am again able to fetch data from rest API.
If I reverse the order of APIs that is with wrong credentials first and then ith right creentials then I get 407 erro on 1st API hit which is correct.
How is this possible.
Here is my Code :
1st Call :
Dim myHttpWebRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(uri), HttpWebRequest)
myHttpWebRequest.Headers.Add("Proxy-Authorization", "Basic dXNlcjE6UGFzc3dvcmQx")
myHttpWebRequest.Method = "GET"
myHttpWebRequest.AutomaticDecompression = DecompressionMethods.Deflate Or DecompressionMethods.GZip Or DecompressionMethods.None
Dim myHttpWebResponse = GetresponseForRequest(myHttpWebRequest)
2nd Call
Dim myHttpWebRequest1 As HttpWebRequest = DirectCast(HttpWebRequest.Create(uri), HttpWebRequest)
myHttpWebRequest1.Headers.Add("Proxy-Authorization", "Basic dXNlcjE8UKFzc3dvcmQn")
myHttpWebRequest1.Method = "GET"
myHttpWebRequest1.AutomaticDecompression = DecompressionMethods.Deflate Or DecompressionMethods.GZip Or DecompressionMethods.None
Dim myHttpWebResponse1 = GetresponseForRequest(myHttpWebRequest)

Related

404 error while getting server response vb.net

I'm a totally beginner with webrequest, so I have no idea about what cause the error I get.
I try to login on a form following the microsoft tutorial for webrequest, but when I want to get the server response, I have the following error :
"the remote server returned an error (404) not found"
So I know that the URL I use actually exist and then wonder which part of the code is bad. Maybe it's because I'm doing an HTTPS request unlike the tutorial and it changes something ?
Also, I'm a little confused by getting directly the answer from the server : shouldn't there be kind of a trigger to know when the server answered ?
Dim request = WebRequest.Create("https://ssl.vocabell.com/mytica2/login")
request.Credentials = CredentialCache.DefaultCredentials
request.Method = "POST"
Dim byteArray = Encoding.UTF8.GetBytes("_username=x&_password=x")
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length
Dim dataStream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim reponse = request.GetResponse() 'ERROR
MsgBox(CType(reponse, HttpWebResponse).StatusDescription)
Using ds = reponse.GetResponseStream
Dim reader = New StreamReader(ds)
MsgBox(reader.ReadToEnd)
End Using
reponse.Close()
Thank you for your time, and if you have any relevant tutorial on the topic I would be glad to read it !
The page you've mentioned does exist and uses HTTPS, but if you look at the form tag within it, it's like this:
<form class="login-form form-horizontal" action="/mytica2/login_check" method="POST">
This means it doesn't post the form back to the same URL as the page, instead it sends it to the URL contained within that "action" attribute. If you're trying to use your code to simulate the submission of the login form then it looks like you need to send your POST request to https://ssl.vocabell.com/mytica2/login_check instead.

Proxy-Authorization Header not working in HttpWebRequest, in case of Rest Request to a URL

I am using HttpWebRequest for fetching data from Rest API.
I have proxy applied on my system and I need to authorize with Proxy Server in order to fetch data from Rest API.
For Authorization, I am using Proxy-Authorization header with value as "Basic base64_credentials".
But even after sending these credentials, I am not able to authorize with Proxy Server.
If I hit same Rest API from Postman by giving Proxy-Authorization header it works perfectly.
Here is my code :
Dim myHttpWebRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(uri), HttpWebRequest)
myHttpWebRequest.Headers.Add("Proxy-Authorization", "Basic dXNlcjE6UGFzc3dvcmQx")
myHttpWebRequest.Method = "GET"
myHttpWebRequest.AutomaticDecompression = DecompressionMethods.Deflate Or DecompressionMethods.GZip Or DecompressionMethods.None
Dim myHttpWebResponse = GetresponseForRequest(myHttpWebRequest)
What could be the possible reason for this?
I have not touched HttpWebRequest.Proxy settings.

Adding Header to Web Request throws an error

When attempting to add a value to the header of an HTTP request, I get the following error:
Specified value does not have a ':' separator. Parameter name: header
To add the header I am using the following code:
Dim request As HttpWebRequest = WebRequest.Create(url)
Dim soapXML As New XmlDocument()
soapXML.LoadXml(soapEnvelope)
request.Headers.Add("SOAPAction", action)
request.Method = "POST"
request.ContentType = "text/xml;charset=""utf-8"""
request.Accept = "text/xml"
Using stream As Stream = request.GetRequestStream()
soapXML.Save(stream)
End Using
I have been looking all over the place and haven't found anybody who has had any similar errors, outside of this SO question: using httpRequest headers?
Which didn't help much.
Does anybody know what I might be doing wrong here?
EDIT 1: Added all the code that I currently have that creates my web request.

500 error on api request. works fine when called through browser

Hello I have an api which authenticates the user login . Now when I hit this rest service in my browser it displays the result but when I try to do this using my code it gives in 500 error. Please help me with this .
My Api: http://abhinavevent2014.sched.org/api/auth/login?api_key=1309658400d57c8cfc6081f8361de52c&username=abhinavm#test.com&password=test
string url = #"http://abhinavevent2014.sched.org/api/auth/login?api_key=1309658400d57c8cfc6081f8361de52c&username=abhinavm#test.com&password=test";
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
WebRequest requestfriend = WebRequest.Create(url);
WebResponse responsefriend = requestfriend.GetResponse(); ( This is where it blows)
Stream streamResponse = responsefriend.GetResponseStream();
StreamReader streamReaderResponse = new StreamReader(streamResponse, encode);
jsonResult = streamReaderResponse.ReadToEnd();
return jsonResult.ToString();
You need to use User-Agent in the headers, as the documentation says:
You must also send requests with a User-Agent in the headers, as Sched
filters out requests with no User-Agent.
Sched API Doc
Browser automatically does that, so it can retrieve the data.

No response after authentication success in pentaho?

I try to use Basic Authentication to make a login into Pentaho by using .NET code
My code is:
Dim request = WebRequest.Create("http://x.x.x.x:8080/pentaho/Home")
Dim authInfo As String = Convert.ToString(userName) & ":" & Convert.ToString(userPassword)
authInfo = Convert.ToBase64String(Encoding.[Default].GetBytes(authInfo))
request.Headers("Authorization") = "Basic " & authInfo
Dim response As WebResponse = request.GetResponse()
You can see that I pass the authorization header with Basic username:password with Base64 encode.
for the last line of above code
Dim response As WebResponse = request.GetResponse()
you can see that I get the response from pentaho server then assign to variable response
In running, I found that Pentaho can return response and put the value to variable response.It seem okay that the authentication is passed and I login successfully
but when I input a link http://x.x.x.x:8080/pentaho/Home the login page still prompt and I need to login again....
Do you know what code I missing so that the Pentaho cannot login auto after above code?
Thanks you much!!!!!
Have you tried putting it as parameters in the url i.e :
?userid=joe&password=password
However, looking at it again it seems if you're successfully getting a logged in response, then the next request you are not logged in then you have not preserved the session/cookie between those two requests. Easily done in java with httpclient, not so sure about .net