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);
Srinivas
  • 135
  • 4
  • 9
  • take a look at this stackoverflow post http://stackoverflow.com/questions/5401501/how-to-post-data-to-specific-url-using-webclient-in-c-sharp – MethodMan Nov 20 '12 at 18:32
  • Can you try comparing the request/response with a proxy HTTP monitoring tool like [Fiddler](http://www.fiddler2.com/)? – mellamokb Nov 20 '12 at 18:37
  • 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 command – Srinivas Nov 20 '12 at 20:35

1 Answers1

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);
Srinivas
  • 135
  • 4
  • 9