I have a POST request, that return code 302.
string FormParams = "Some_string";
byte[] SomeBytes = Encoding.UTF8.GetBytes(FormParams);
HttpWebRequest AuthPost = (HttpWebRequest)WebRequest.Create("https://example.com/");
AuthPost.Method = "POST";
AuthPost.AllowAutoRedirect = false;
AuthPost.Accept = "text/html, application/xhtml+xml, */*";
AuthPost.Headers["Referer"] = "https://example.com/";
AuthPost.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; ASU2JS; rv:11.0) like Gecko";
AuthPost.ContentType = "application/x-www-form-urlencoded";
AuthPost.Headers["Accept-Encoding"] = "gzip, deflate";
AuthPost.Headers["DNT"] = "1";
AuthPost.Headers["Connection"] = "Keep-Alive";
AuthPost.Headers["Cookie"] = savedcookie;
AuthPost.Headers["Content-Length"] = SomeBytes.Length.ToString();
AuthPost.Headers["Cache-Control"] = "no-cache";
Stream postStream = await AuthPost.GetRequestStreamAsync();
postStream.Write(SomeBytes, 0, SomeBytes.Length);
postStream.Flush();
HttpWebResponse AuthPostResponse = (HttpWebResponse)await AuthPost.GetResponseAsync();
So I need manage returned cookie before redirecting.
How can I turn off auto redirect or manage cookies?
Use the CookieContainer Property/Class. See MSDN
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
You can reuse the container in other requests.
Related
I have an application written in C# that use HttpWebRequest to send the request with body.
To add text to request I use this code:
WebRequest webRequest = WebRequest.Create(responseUri);
((HttpWebRequest)webRequest).Referer = responseUri;
((HttpWebRequest)webRequest).Host = "sts.mycompany.com";
((HttpWebRequest)webRequest).KeepAlive = true;
((HttpWebRequest)webRequest).AllowAutoRedirect = true;
((HttpWebRequest)webRequest).UseDefaultCredentials = true;
((HttpWebRequest)webRequest).UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36";
webRequest.ContentType = "application/x-www-form-urlencoded";
((HttpWebRequest)webRequest).Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
((HttpWebRequest)webRequest).Headers.Add("Cache-Control", "max-age=0");
((HttpWebRequest)webRequest).Headers.Add("Accept-Encoding", "gzip, deflate, br, peerdist");
((HttpWebRequest)webRequest).Headers.Add("Accept-Language", "en-US,en;q=0.5");
//((HttpWebRequest)webRequest).Headers.Add("Accept-Charset", "ISO-8859-2,utf-8;q=0.7,*;q=0.3");
((HttpWebRequest)webRequest).Headers.Add("Upgrade-Insecure-Requests", #"1");
((HttpWebRequest)webRequest).Headers.Add("DNT", #"1");
((HttpWebRequest)webRequest).CookieContainer = CookieContainer;
foreach (var cookie in CookieContainer.GetCookies(new Uri(responseUri)))
{
Console.WriteLine(cookie.ToString());
}
((HttpWebRequest)webRequest).Credentials = GetNetworkCredentials();
((HttpWebRequest)webRequest).AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
webRequest.Method = requestType.ToString();
string msg = String.Format("UserName={0}&Password={1}&AuthMethod=FormsAuthentication", System.Net.WebUtility.UrlEncode(username), System.Net.WebUtility.UrlEncode(password));
webRequest.ContentLength = msg.Length;
Stream reqStream = webRequest.GetRequestStream();
byte[] msgb = System.Text.Encoding.UTF8.GetBytes(msg);
reqStream.Write(msgb, 0, msgb.Length);
reqStream.Close();
var response = (HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string Result = sr.ReadToEnd();
var originalString = response.ResponseUri.OriginalString;
response.Close();
The error is in this line, when I want to add text to body:
byte[] msgb = System.Text.Encoding.UTF8.GetBytes(msg);
reqStream.Write(msgb, 0, msgb.Length);
error: Cannot send a content-body with this verb-type
and any additional informations.
I am trying to extract data from a government site which is rendered on display of a pop up. I checked the network console and got the POST request URL and able to replicate the request-response on Postman. Now I am trying to make the call programmatically. I tried using the default code generated by Postman but it did not work.
I am writing the code in C# and I am able to get the response but I am not able to get the correct encoding to extract the response.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://saubhagya.gov.in/dashboard/data/dashboard_saubhagya");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "application/json, text/javascript, */*; q=0.01";
request.Host = "saubhagya.gov.in";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
request.Headers.Add("Accept-Encoding: gzip, deflate");
request.Headers.Add("Accept-Language: en-US,en;q=0.9");
request.Headers.Add("Cache-Control: no-cache");
request.Headers.Add("Pragma: no-cache");
request.Headers.Add("Origin: http://saubhagya.gov.in");
request.Referer = "http://saubhagya.gov.in/";
String CookieStr = "_ga=GA1.3.590075225.1533991967; _gid=GA1.3.790472263.1533991967; saubhagyasession=BHxX%2FFfttUfxM7JhoIruGVzdq0m%2F4sGeTn95c%2BUB%2BGvJok9PkS3g9pR8vLVfeEJ1XB8UULGNThvbAeN5HfAu%2FE6qt%2F5X3qL8Yla4my0qmxSmz6Q9ztpLztCD0PyY17uWDnJgkSjSt%2BSF0B5Xh32SUsxBXHH%2BeFGwtIXdAnzSLcxC0MO8KZSiE2io4ksZO6AZ31YSxnGei6CluQzg4fCFgXvVwR4%2F00%2FKAbf0MnhLwaTtXxD0jngmDv3Rjy8enD87c20vwObHGTgcLC3KQoh2lw5L1WRF1lVLlpjzLrUoeJV3cD8o0c15bT5SA%2FV1Y8OqFPhqhpr0%2BzzG%2FbAVs6OKMmLiokl7hHrPx5NECDsmY3KzmCkNHka%2B1ueEWTv%2FTOUqH2hll2A8485gFhqFgnrh%2FKkhOb6I8lChI2QQoyHr%2B9U%3D92add88ce105d8b3ec1dd72efa1dd7ec9b9f1e52";
CookieContainer cookiecontainer = new CookieContainer();
string[] cookies = CookieStr.Split(';');
foreach (string cookie in cookies)
cookiecontainer.SetCookies(new Uri("http://saubhagya.gov.in/dashboard/data/dashboard_saubhagya"), cookie);
request.CookieContainer = cookiecontainer;
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = "ci_csrf_token=&state=35&district=638&village=645115&vtype=&discom=&search_text=&uuid=&maptype=states&kyroargs=&drilldownkey=&kyroclickid=&kyrorefreshid=&page=dashboard_saubhagya";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(response.CharacterSet)))
{
var result = sr.ReadToEnd();
}
I get an encoded/junk string as output.
Requesting for help!
In the header you state that you accept gzip, but the response is not decompressed on your end, so just add:
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
Then you can remove this line because headers will be added automatically:
//request.Headers.Add("Accept-Encoding: gzip, deflate");
I have a https website, when I access it via the browser and check the cookies (via firebug), it gives me 3 cookies. but when I make the request via c# code, the cookie count is 2. one of them is missing. Can someone help what is happening ?
public void Login(string url)
{
//Create request to URL.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create( url);
//Set request headers.
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.5");
request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
request.ContentType = "application/x-www-form-urlencoded";
request.KeepAlive = true;
request.Headers.Add("Upgrade-Insecure-Requests", #"1");
//Set request method
request.Method = "POST";
request.ServicePoint.Expect100Continue = false;
// Set request body.
string body = #"";
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body);
request.ContentLength = postBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();
CookieContainer cookieJar = new CookieContainer();
request.CookieContainer = cookieJar;
//Get response to request.
var response = (HttpWebResponse)request.GetResponse();
var xx = ReadResponse(response);
int cookieCount = cookieJar.Count;
var items = cookieJar.List();
fixCookies(response);
items = cookieJar.List();
}
private static void fixCookies(HttpWebResponse response)
{
for (int i = 0; i < response.Headers.Count; i++)
{
string name = response.Headers.GetKey(i);
if (name != "Set-Cookie")
continue;
string value = response.Headers.Get(i);
foreach (var singleCookie in value.Split(','))
{
//I get only two values here.
}
}
}
The error says:
"The remote server returned an error:
(http://www.tgv.com.my/movies/man-city-v-arsenal-HO00005174)
Forbidden"
below is my code:
string url = https://translate.google.com/translate_a/single?client=t&sl=en&tl=vi&hl=vi&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&ie=UTF-8&oe=UTF-8&otf=1&srcrom=1&ssel=0&tsel=0&kc=5&tk=520987|10880&q=" + keyword;
var request = (HttpWebRequest)WebRequest.Create(url);
WebProxy proxy = (WebProxy)WebProxy.GetDefaultProxy();
if (proxy.Address != null)
{
proxy.Credentials = proxy.Credentials = new NetworkCredential("username", "pw");
WebRequest.DefaultWebProxy = new System.Net.WebProxy(proxy.Address, proxy.BypassProxyOnLocal, proxy.BypassList, proxy.Credentials);
}
request.Proxy = proxy;
var postData = "";
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "text/html; charset=UTF-8";
request.ContentLength = data.Length;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
return responseString;
Thanks!
Using your Url, it's not a POST request, it's a GET request and could be done like this:
string url = "https://translate.google.com/translate_a/single?client=t&sl=de&tl=en&hl=de&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=2&ssel=0&tsel=0&kc=4&tk=767774.885916&q=hallo%20du";
var request = (HttpWebRequest)WebRequest.Create(url);
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
But if your q should have another value, other values must be changed too, otherwise you will have the Error 403 whitch tells you, that you do not have permission to do your request.
Using Google Translate, consider having a look at the Google Translate API.
There you can do your request like that:
https://www.googleapis.com/language/translate/v2?key=YOUR_API_KEY&q=hello%20world&source=en&target=de
But this is a payd service...
How login to the resource with SSL? I sent data with help HttpWebRequest on url. In half of cases return redirect to login page and dont't get cookies.
Maybe, make request on 443 port? If yes, how this make with help HttpWebRequest?
ServicePointManager
.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
const string root = #"https://****/";
var loginUrl = string.Format(#"{0}csologin/login.jsf", root);
var login = Settings.Default.PacerLogin;
var pass = Settings.Default.PacerPassword;
var postData =
string.Format(
#"login=login&login:loginName={0}&login:password={1}&login:clientCode=&login:j_idt139=&javax.faces.ViewState=stateless",
login, pass);
var request = (HttpWebRequest)WebRequest.Create(loginUrl);
request.CookieContainer = this.cookieContainer;
var ascii = new ASCIIEncoding();
byte[] postBytes = ascii.GetBytes(postData);
request.Proxy = null;
request.Credentials = new NetworkCredential(login, pass);
request = SetHeaders(request, loginUrl);
request.ContentLength = postBytes.Length;
request.Method = WebRequestMethods.Http.Post;
Stream postStream = request.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Flush();
postStream.Close();
And Headers
request.UserAgent =
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36";
request.Headers.Add("Accept-Language", "en-GB,en-US;q=0.8,en;q=0.6,ru;q=0.4");
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.KeepAlive = true;
request.Headers["Cache-Control"] = "max-age=0";
request.AllowAutoRedirect = allowRedirect;
request.Connection =
request.Referer = referer;
request.ContentType = "application/x-www-form-urlencoded";
request.ServicePoint.Expect100Continue = false;