C# Expect100Continue header request - c#

I am facing the problem with posting username and password with different domains - one submits the form successfully while the other doesn't(the form data is empty)! The html code on both domains is the same. Here is the sample code- the commented domain doesn't post: Any Help is greatly appreciated!
Note: the domain that runs on nginx posts data successfully while the other on apache doesn't if at all it has got something to do with servers
public class CookieAwareWebClient : System.Net.WebClient
{
private System.Net.CookieContainer Cookies = new System.Net.CookieContainer();
protected override System.Net.WebRequest GetWebRequest(Uri address)
{
System.Net.WebRequest request = base.GetWebRequest(address);
if (request is System.Net.HttpWebRequest)
{
var hwr = request as System.Net.HttpWebRequest;
hwr.CookieContainer = Cookies;
}
return request;
}
}
# Main function
NameValueCollection postData = new NameValueCollection();
postData.Add("username", "abcd");
postData.Add("password", "efgh");
var wc = new CookieAwareWebClient();
//string url = "https://abcd.example.com/service/login/";
string url = "https://efgh.example.com/service/login/";
wc.DownloadString(url);
//writer.WriteLine(wc.ResponseHeaders);
Console.WriteLine(wc.ResponseHeaders);
byte[] results = wc.UploadValues(url, postData);
string text = System.Text.Encoding.ASCII.GetString(results);
Console.WriteLine(text);

The problem was with Expect100Continue header being added automatically each time when a request was made through the program which wasn't handled well on Apache. You have to set the Expect100Continue to false each time when a request is made in the following way. Thanks for the Fiddler lead although I could see it through the dumpcap tool on Amazon EC2 instance! Here is the solution!
# Main function
NameValueCollection postData = new NameValueCollection();
postData.Add("username", "abcd");
postData.Add("password", "efgh");
var wc = new CookieAwareWebClient();
var uri = new Uri("https://abcd.example.com/service/login/");
var servicePoint = ServicePointManager.FindServicePoint(uri);
servicePoint.Expect100Continue = false;
wc.DownloadString(uri);
Console.WriteLine(wc.ResponseHeaders);
byte[] results = wc.UploadValues(uri, postData);
string text = System.Text.Encoding.ASCII.GetString(results);
Console.WriteLine(text);

Related

Some Nunit tests are failing in groups but pass when run individually in other env

I have been trying to hit a Web controller directly to upload csv files using Nunit, Specflow and Restsharp but the tests are failing in groups but passes when individually. Another thing which we noticed is these are working fine locally as in running the app as localhost but not in other env. All other test groups are passing in that env. I have seen couple of posts which suggested not to use static properties to store values but i have tried that as well. we also added delay between threads but thats not working either
please can anyone help on this one or anyone who have faced this?
The error I am getting is timeout. if you accces it from the browser then there are no issues
I am using context injection to pass values from one another in step def file.
Here is what the code looks like
public (IRestResponse response,string RVtoken) GetRVtoken()
{
var client = new RestClient(ConfigurationManager.AppSettings["Web_URL"] +"Account/SignIn");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
var html = new HtmlDocument();
html.LoadHtml(response.Content.ToString());
var RVtoken = html.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/div[1]/form[1]/input[1]/#value[1]").Attributes["value"].Value;
return (response, RVtoken);
}
public (CookieContainer cookieContainer, Uri target) GetAspxAuthToken(string email,string password, IRestResponse SignInResponse, string RVtoken)
{
string ReturnUrl = ConfigurationManager.AppSettings["Web_URL"] + "Account/SignIn?ReturnUrl=%2F";
var client = new RestClient(ReturnUrl);
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(ReturnUrl);
httpRequest.CookieContainer = cookieContainer;
httpRequest.Method = Method.POST.ToString();
string postData = string.Format("Email={0}&Password={1}&__RequestVerificationToken={2}&RememberMe=false", email, password, RVtoken);
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = byteArray.Length;
Uri target = new Uri(ReturnUrl);
httpRequest.CookieContainer.Add(new Cookie(SignInResponse.Cookies[0].Name, SignInResponse.Cookies[0].Value) { Domain = target.Host });
httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
Stream dataStream = httpRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
HttpWebResponse myresponse = (HttpWebResponse)httpRequest.GetResponse();
return (cookieContainer,target);
}
public IRestResponse Uploadflex(string flexType,string filepath,IRestResponse SignInResponse)
{
RestClient newclient = newclient = new RestClient(ConfigurationManager.AppSettings["Web_URL"] + "audiences/" + flexType + "/upload?utc=1580117555355");
var newrequests = new RestRequest(Method.POST);
newrequests.AddFile("targetAreaFile",filepath);
newrequests.AddCookie(SignInResponse.Cookies[0].Name, SignInResponse.Cookies[0].Value);
newrequests.AddCookie(User.cookieContainer.GetCookies(User.target)[".aspxauth"].Name.ToString(), User.cookieContainer.GetCookies(User.target)[".aspxauth"].Value.ToString());
newrequests.AddParameter("name", "targetAreaFile");
newrequests.AddParameter("Content-Type", "application/vnd.ms-excel");
newrequests.AddParameter("Content-Disposition", "form-data");
var response = newclient.Execute(newrequests);
return response;
}
ok, so the issue was I was creating differnet sessions for these tests but somehow cookies were not clearing so I put all the tests under same session and it works magically.

HttpClient posting form to a web page returns the same page

I have a page on a site designed for adding a certain entity. What I'm trying to do is to add this entity using C# HttpClient. My sequence of steps looks like this:
First I'm authenticate using the client:
public static async Task<CookieCollection> WebPortalLogin(string baseURI, string phoneNo, string pin)
{
var cookies = new CookieContainer();
var handler = new HttpClientHandler()
{
CookieContainer = cookies
};
var client = new HttpClient(handler);
var content = new FormUrlEncodedContent(new[]{
new KeyValuePair<string,string>("page","login"),
new KeyValuePair<string,string>("noStaticBox",""),
new KeyValuePair<string,string>("username",phoneNo),
new KeyValuePair<string,string>("password",pin),
new KeyValuePair<string,string>("login","Увійти"),
new KeyValuePair<string,string>("_reqNo","0"),
});
var response = await client.PostAsync(baseURI, content);
response.EnsureSuccessStatusCode();
var stringResponse = response.Content.ReadAsStringAsync().Result;
var cookieJar = cookies.GetCookies(new Uri(baseURI));
return cookieJar;
}
Then I send POST request to edit page with all data I want to save:
public static async Task<HttpResponseMessage> AddCar(string baseURI, string phoneNo, CookieCollection cookieJar, string carNo, string owner)
{
var cookieContainer = new CookieContainer();
var client = new HttpClient(new HttpClientHandler() { CookieContainer = cookieContainer });
var content = new FormUrlEncodedContent(new[]{
new KeyValuePair<string,string>("carNo",carNo),
new KeyValuePair<string,string>("userName",owner),
new KeyValuePair<string,string>("page","carNumbers"),
new KeyValuePair<string,string>("submit","Додати"),
new KeyValuePair<string,string>("operation","addCar"),
new KeyValuePair<string,string>("_reqNo","0")
});
cookieContainer.Add(new Uri(baseURI), cookieJar);
var response = await client.PostAsync(baseURI, content);
var stringResponse = response.Content.ReadAsStringAsync().Result;
return response;
}
However, this POST request does nothing, and in response I have this very edit page, although when I add this entity in normal way (via web site), I get empty response and entity is successfully saved. Already checked cookies - they're all right. The only thing I can think of is request headers, but successful POST has only regular ones, like Accept, Accept-Encoding etc. What are my possible mistakes and how can I get it posted? Note: all connections use HTTPS.
ok, after messing a couple of hours I've discovered that the problem is in one of request parameters and not related to httpclient

Log in to Drupal site via C# application

I want to log in to my Drupal site via a C# application and have been using OAuth and Services with no success. Is there a way to log in with a session from C#? I need to do this to access my private files on my Drupal site.
I tried to use the code below from a user here on stackoverflow with now success. I get 403 when using the code.
public class CookieAwareWebClient : WebClient
{
private CookieContainer cookie = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = cookie;
}
return request;
}
}
var client = new CookieAwareWebClient();
client.BaseAddress = #"https://www.mysite.com/";
var loginData = new NameValueCollection();
loginData.Add("login", user);
loginData.Add("password", password);
client.UploadValues("login.php", "POST", loginData); // ERROR HERE
//Now you are logged in and can request pages
string htmlSource = client.DownloadString("index.php");
There is no login.php in drupal. I think you need to check these examples: (RPC / Services 3)
https://drupal.org/node/308629
http://www.technogumbo.com/2012/05/Drupal-7-Services-3-REST-Server-C-Sharp-Example/

Simulating a full post request

I am trying to simulate a post request using WebClient; However, When logging in using Firefox and debugging the request with firebug i find that after the POST request it automatically do some GET requests while using my code only do the POST request
MY CODE
//Handler is an overridden WebClient Class
private async Task<byte[]> Post(string uri, string[] data)
{
var postData = new NameValueCollection();
foreach (var info in data.Select(var => var.Split('=')))
{
postData.Add(info[0], info[1]);
}
return await Handler.UploadValuesTaskAsync(new Uri(uri), postData);
}
I know this isn't what you are asking for, AND its in VB, but hopefully it can help to point you in the right direction. It is what I use to make post requests on one of our websites. It works for simulating the POST data, hopefully you can incorporate some of that into what you are doing.
Dim postData As String = String.Format("RedirectLocation=RequestMethod=&username={0}&password={1}", _username, _password)
Dim _loginRequest As HttpWebRequest = WebRequest.Create(loginurl)
With _loginRequest
.Method = "POST"
.ContentLength = postData.Length
.ContentType = "application/x-www-form-urlencoded"
.KeepAlive = True
.AllowAutoRedirect = False
.CookieContainer = New CookieContainer
Using writer As New StreamWriter(.GetRequestStream)
writer.Write(postData)
End Using
.Timeout = tsTimeOut.TotalMilliseconds
_loginResponse = .GetResponse()
End With

Problems authenticating to website from code

I am trying to write code that will authenticate to the website wallbase.cc. I've looked at what it does using Firfebug/Chrome Developer tools and it seems fairly easy:
Post "usrname=$USER&pass=$PASS&nopass_email=Type+in+your+e-mail+and+press+enter&nopass=0" to the webpage "http://wallbase.cc/user/login", store the returned cookies and use them on all future requests.
Here is my code:
private CookieContainer _cookies = new CookieContainer();
//......
HttpPost("http://wallbase.cc/user/login", string.Format("usrname={0}&pass={1}&nopass_email=Type+in+your+e-mail+and+press+enter&nopass=0", Username, assword));
//......
private string HttpPost(string url, string parameters)
{
try
{
System.Net.WebRequest req = System.Net.WebRequest.Create(url);
//Add these, as we're doing a POST
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
((HttpWebRequest)req).Referer = "http://wallbase.cc/home/";
((HttpWebRequest)req).CookieContainer = _cookies;
//We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value&
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(parameters);
req.ContentLength = bytes.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length); //Push it out there
os.Close();
//get response
using (System.Net.WebResponse resp = req.GetResponse())
{
if (resp == null) return null;
using (Stream st = resp.GetResponseStream())
{
System.IO.StreamReader sr = new System.IO.StreamReader(st);
return sr.ReadToEnd().Trim();
}
}
}
catch (Exception)
{
return null;
}
}
After calling HttpPost with my login parameters I would expect all future calls using this same method to be authenticated (assuming a valid username/password). I do get a session cookie in my cookie collection but for some reason I'm not authenticated. I get a session cookie in my cookie collection regardless of which page I visit so I tried loading the home page first to get the initial session cookie and then logging in but there was no change.
To my knowledge this Python version works: https://github.com/sevensins/Wallbase-Downloader/blob/master/wallbase.sh (line 336)
Any ideas on how to get authentication working?
Update #1
When using a correct user/password pair the response automatically redirects to the referrer but when an incorrect user/pass pair is received it does not redirect and returns a bad user/pass pair. Based on this it seems as though authentication is happening, but maybe not all the key pieces of information are being saved??
Update #2
I am using .NET 3.5. When I tried the above code in .NET 4, with the added line of System.Net.ServicePointManager.Expect100Continue = false (which was in my code, just not shown here) it works, no changes necessary. The problem seems to stem directly from some pre-.Net 4 issue.
This is based on code from one of my projects, as well as code found from various answers here on stackoverflow.
First we need to set up a Cookie aware WebClient that is going to use HTML 1.0.
public class CookieAwareWebClient : WebClient
{
private CookieContainer cookie = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
request.ProtocolVersion = HttpVersion.Version10;
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = cookie;
}
return request;
}
}
Next we set up the code that handles the Authentication and then finally loads the response.
var client = new CookieAwareWebClient();
client.UseDefaultCredentials = true;
client.BaseAddress = #"http://wallbase.cc";
var loginData = new NameValueCollection();
loginData.Add("usrname", "test");
loginData.Add("pass", "123");
loginData.Add("nopass_email", "Type in your e-mail and press enter");
loginData.Add("nopass", "0");
var result = client.UploadValues(#"http://wallbase.cc/user/login", "POST", loginData);
string response = System.Text.Encoding.UTF8.GetString(result);
We can try this out using the HTML Visualizer inbuilt into Visual Studio while staying in debug mode and use that to confirm that we were able to authenticate and load the Home page while staying authenticated.
The key here is to set up a CookieContainer and use HTTP 1.0, instead of 1.1. I am not entirely sure why forcing it to use 1.0 allows you to authenticate and load the page successfully, but part of the solution is based on this answer.
https://stackoverflow.com/a/10916014/408182
I used Fiddler to make sure that the response sent by the C# Client was the same as with my web browser Chrome. It also allows me to confirm if the C# client is being redirect correctly. In this case we can see that with HTML 1.0 we are getting the HTTP/1.0 302 Found and then redirects us to the home page as intended. If we switch back to HTML 1.1 we will get an HTTP/1.1 417 Expectation Failed message instead.
There is some information on this error message available in this stackoverflow thread.
HTTP POST Returns Error: 417 "Expectation Failed."
Edit: Hack/Fix for .NET 3.5
I have spent a lot of time trying to figure out the difference between 3.5 and 4.0, but I seriously have no clue. It looks like 3.5 is creating a new cookie after the authentication and the only way I found around this was to authenticate the user twice.
I also had to make some changes on the WebClient based on information from this post.
http://dot-net-expertise.blogspot.fr/2009/10/cookiecontainer-domain-handling-bug-fix.html
public class CookieAwareWebClient : WebClient
{
public CookieContainer cookies = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address);
var httpRequest = request as HttpWebRequest;
if (httpRequest != null)
{
httpRequest.ProtocolVersion = HttpVersion.Version10;
httpRequest.CookieContainer = cookies;
var table = (Hashtable)cookies.GetType().InvokeMember("m_domainTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, cookies, new object[] { });
var keys = new ArrayList(table.Keys);
foreach (var key in keys)
{
var newKey = (key as string).Substring(1);
table[newKey] = table[key];
}
}
return request;
}
}
var client = new CookieAwareWebClient();
var loginData = new NameValueCollection();
loginData.Add("usrname", "test");
loginData.Add("pass", "123");
loginData.Add("nopass_email", "Type in your e-mail and press enter");
loginData.Add("nopass", "0");
// Hack: Authenticate the user twice!
client.UploadValues(#"http://wallbase.cc/user/login", "POST", loginData);
var result = client.UploadValues(#"http://wallbase.cc/user/login", "POST", loginData);
string response = System.Text.Encoding.UTF8.GetString(result);
You may need to add the following:
//get response
using (System.Net.WebResponse resp = req.GetResponse())
{
foreach (Cookie c in resp.Cookies)
_cookies.Add(c);
// Do other stuff with response....
}
Another thing that you might have to do is, if the server responds with a 302 (redirect) the .Net web request will automatically follow it and in the process you might lose the cookie you're after. You can turn off this behavior with the following code:
req.AllowAutoRedirect = false;
The Python you reference uses a different referrer (http://wallbase.cc/start/). It is also followed by another post to (http://wallbase.cc/user/adult_confirm/1). Try the other referrer and followup with this POST.
I think you are authenticating correctly, but that the site needs more info/assertions from you before proceeding.

Categories

Resources