[英]C# Expect100Continue header request
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. 两个域上的html代码是相同的。 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 注意:在nginx上运行的域成功发布了数据,而在apache上的其他域则完全没有关系,这与服务器有关
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. 问题在于,每次通过程序发出请求时,都会自动添加Expect100Continue标头,而在Apache上处理得不好。 You have to set the Expect100Continue to false each time when a request is made in the following way. 每次以以下方式发出请求时,必须将Expect100Continue设置为false。 Thanks for the Fiddler lead although I could see it through the dumpcap tool on Amazon EC2 instance! 感谢Fiddler的领导,尽管我可以通过Amazon EC2实例上的dumpcap工具看到它! 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.