C#Expect100Continue标头请求 [英] C# Expect100Continue header request
问题描述
我遇到了使用不同域发布用户名和密码的问题 - 一个成功提交表单而另一个没有(表单数据为空)!两个域上的html代码是相同的。以下是示例代码 - 评论域未发布:非常感谢任何帮助!
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!
注意:在nginx上运行的域成功发布数据而另一个在apache不会,如果它与服务器有关
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);
推荐答案
问题在于每次都会自动添加Expect100Continue标头当通过程序提出请求时,在Apache上处理得不好。每次以下列方式发出请求时,您必须将Expect100Continue设置为false。感谢Fiddler领导,虽然我可以通过Amazon EC2实例上的dumpcap工具看到它!这是解决方案!
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);
这篇关于C#Expect100Continue标头请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!