C# Expect100Continue header request
Asked Answered
V

1

1

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);
Verulamium answered 20/11, 2012 at 18:27 Comment(3)
take a look at this stackoverflow post #5402001Commissionaire
Can you try comparing the request/response with a proxy HTTP monitoring tool like Fiddler?Xerophagy
From the response headers I see couple headers missing from Apache response. Will this be a problem? 1. Connection 2. transfer-encoding The response from nginx server is: Connection: keep-alive and transfer-encoding: chunked These are the responses I get through my c# program and when I run curl commandVerulamium
V
1

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);
Verulamium answered 21/11, 2012 at 19:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.