C#Expect100Continue标头请求
我面临着在不同域中发布用户名和密码的问题-一个域成功提交表单,而另一个域未成功提交表单(表单数据为空)!两个域上的html代码相同。下面是示例代码-评论的域没有发布:非常感谢任何帮助 注意:在nginx上运行的域成功地发布了数据,而在apache上运行的另一个域则没有,如果它与服务器有关的话C#Expect100Continue标头请求,c#,http,httpwebrequest,webclient,httpcookie,C#,Http,Httpwebrequest,Webclient,Httpcookie,我面临着在不同域中发布用户名和密码的问题-一个域成功提交表单,而另一个域未成功提交表单(表单数据为空)!两个域上的html代码相同。下面是示例代码-评论的域没有发布:非常感谢任何帮助 注意:在nginx上运行的域成功地发布了数据,而在apache上运行的另一个域则没有,如果它与服务器有关的话 public class CookieAwareWebClient : System.Net.WebClient { private System.Net.CookieContainer Cook
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的指导,尽管我可以通过AmazonEC2实例上的dumpcap工具看到它!这就是解决办法
# 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);
看看这篇stackoverflow文章,您可以尝试将请求/响应与代理HTTP监视工具进行比较吗?从响应头中,我看到Apache响应中缺少两个头。这会是个问题吗?1.连接2。传输编码来自nginx服务器的响应是:Connection:keep alive和transfer encoding:chunked这些是我通过c#程序运行curl命令时得到的响应