I'm trying to get Cookie from HTTP get request with HttpWebReqquest object in c# but I don't find it.
if i execute the next http get reuqest in the browser:
https://www.lefrecce.it/msite/api/solutions?origin=Milano Centrale&destination=Roma Termini&arflag=A&adate=25/01/2021 0:00:00&atime=10&adultno=1&childno=0&direction=A&frecce=false&onlyRegional=false
you can see clearly that there are 3 Set-Cookies:
enter image description here
but when I execute it with HttpWebRequest:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
res = reader.ReadToEnd();
}
I can see in the headers only 2 cookies and the third one is missing:
enter image description here
so how can I get this cookie from?
thanks.
Just tried this in net5 and I see three cookies:
Related
I want to get from Jenkins the last build status via C# code, once I'm triggered a "GET" request it returns me 403 forbidden.
public void GetJsonByGet(string url)
{
string json = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("JenkinsEndPoint");
request.Credentials = new NetworkCredential("User","Password"); //CredentialCache.DefaultCredentials;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
json = reader.ReadToEnd();
}
}
Check your jenkins has csrf activated or not. if it does you have to implement a call to get the csrf token and use this token for your next requests
I am trying to read a page from a website that logs me in automatically. The problem is the site when launched first gives a page saying "Logging you in" with a loading icon. That happens for about 5 seconds or so and than the actual page loads.
When I run my code it only gets the logon page with icon code and not the actual page after the logon.
Anyway I can get both?
Thanks
WebRequest request = WebRequest.Create("https://server1/test");
request.Credentials = CredentialCache.DefaultCredentials;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream dataStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(dataStream))
{
Console.WriteLine(reader.ReadToEnd());
}
}
}
I'm trying to get the order book from GDAX (link to documentation of the call) but when doing it from the c# executable I always get Error 400 - Bad request.
When taking the actual URL and pasting it into my browser, it works fine.
String URL = "https://api.gdax.com/products/BTC-USD/book?level=2";
WebRequest request = WebRequest.Create(URL);
WebResponse response = request.GetResponse();
The actual issue with your API call is , the API is expecting a user-agent string while making the request: Below is the code in working condition:
try
{
String URL = "http://api.gdax.com/products/BTC-USD/book?level=2";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.UserAgent = ".NET Framework Test Client";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
var encoding = ASCIIEncoding.ASCII;
using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
{
string responseText = reader.ReadToEnd();
}
}
catch(WebException ex)
{
HttpWebResponse xyz = ex.Response as HttpWebResponse;
var encoding = ASCIIEncoding.ASCII;
using (var reader = new System.IO.StreamReader(xyz.GetResponseStream(), encoding))
{
string responseText = reader.ReadToEnd();
}
}
Basically ProtocolError indicates that you have received the response but there is an error related to protocol, which you can observe, when you read the response content from exception. I have added catch to handle the exception and read ex.Response (which is HttpWebResponse) and could see that the API is asking for user-agent to be suppllied while making the call. I got to see the error as "{"message":"User-Agent header is required."}"
You can ignore the code inside the exception block, I used it only to see what is the actual response message, which contains actual error details
Note: I have boxed WebRequest to HttpWebRequest to have additional http protocol related properties and most importantly "UserAgent" property which is not available with the WebRequest class.
You need to Accept the certificarte, Google for access to a https webrequest.
Like this
I have to use HttpWebRequest in C#, to retrieve an access token.
My base url redirects me to another url that contains the access token.
Is there a way to use this to get the access token?
This is my code after applying suggestions from the answer:
// Create a request for the URL.
string url = "https://stackexchange.com/oauth/dialog?client_id=2532&scope=no_expiry&redirect_uri=https://stackexchange.com/oauth/login_success";
WebRequest request = WebRequest.Create(
url);
request.Method = "GET";
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams and the response.
reader.Close();
response.Close();
Debug breaks at line: WebResponse response = request.GetResponse();
VS tells me that the server has the a generic error: 500.
If I try with https://www.google.com, no errors.
You can get the resulting url from the ResponseUri property.
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
NaveValueCollection urlParameters = HttpUtility.ParseQueryString(response.ResponseUri.Query);
// extract the access token from the url.
string accessToken = urlParameters["access_token"];
}
I'm pretty RESTless right now because I keep getting incomplete responses from Amazon. I'm using the Product Advertising API, making one ItemLookup request to the server.
The C# code is pretty basic:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
string resultString;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
resultString = sr.ReadToEnd();
}
The number of chars I recieve is 17408- pretty constant but I've seen something around 16k as well.
This is how it always ends:
...ount><CurrencyCode>EUR</CurrencyCode><FormattedPrice>EUR 11,33</FormattedPri
Is there something I don't know about HttpWebRequest or Amazon's API?
the url (don't care about the key) edit: bad idea, limit exceeded...
This worked for me:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
Stream s = response.GetResponseStream();
using (StreamReader sr = new StreamReader(s))
{
s.Flush();
resultString = sr.ReadToEnd();
...
}