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;
Related
so im working on an instagram tool. i need to Create an account with HttpWebRequest but somethings wrong with my code and it doesnt work here is the Code:
username = "something";
password = "something";
mail = " something#mail.com";
name = "something";
string postData = "email=" + mail + "&password=" + password + "&enc_password=%23PWD_INSTAGRAM_BROWSER%3A6%3A1583227313%3AAXFQANTXAGE5jwEKNbpJbvot0SGTp%2Bq7a0ckELnuYQnQLIJa2Th6UwIqdknx%2FDa8R7q1%2F2Bt4scBUrh%2B1aFDKL0H%2Fut5cyi3w1sIEPa1keAuNcNj9nAXo1oFzUYMhCGVB7qerse5hQfMjTVjNa4%3D&username=" + username + "&first_name=" + name + "&seamless_login_enabled=1&tos_version=row&opt_into_one_tap=false";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.instagram.com/accounts/emailsignup/");
byte[] postBytes = Encoding.ASCII.GetBytes(postData);
req.ContentLength = postBytes.Length;
req.CookieContainer = new CookieContainer();
req.Method = "POST";
req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36";
req.Accept = "*/*";
req.ContentType = "application/x-www-form-urlencoded";
req.Referer = "https://www.instagram.com/accounts/emailsignup/";
req.Headers["Accept-Language"] = "de,en-US;q=0.7,en;q=0.3";
req.Headers["Accept-Encoding"] = "gzip, deflate";
req.Headers["X-Requested-With"] = "XMLHttpRequest";
Stream dataStream = req.GetRequestStream();
dataStream.Write(postBytes, 0, postBytes.Length);
dataStream.Flush();
dataStream.Close();
HttpWebResponse webResp = (HttpWebResponse)req.GetResponse(); //error in this line---> The remote server returned an error: (403) Forbidden.
Stream datastream = webResp.GetResponseStream();
StreamReader reader = new StreamReader(datastream);
string s = reader.ReadToEnd();
Console.WriteLine(s);
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.
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...
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.
Recently i made a simple software using c# http request but now it seems there is a problem logging in facebook getting and sending request
here is few lines of code that used work
string email = "email";string pw = "pw";string PostData = String.Format("email={0}&pass={1}", email, pw);
CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.facebook.com");
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
//Get the response from the server and save the cookies from the first request..
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies = response.Cookies;
string getUrl = "https://www.facebook.com/login.php? login_attempt=1";
string postData = String.Format("email={0}&pass={1}", "anything#gmail.com", "yourpassword");
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(cookies); //recover cookies First request
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
string sourceCode = sr.ReadToEnd();
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.DocumentText = sourceCode;
}
First ,saving cookies after sending request then using it to login on facebook.(so that it won't mention "Please enable cookies...")
and Now it doesn't work
Anyone know the issue or should i switch to httpclient (though i haven't used httpclient much)
thanks in advance