简体   繁体   English

C#Expect100Continue标头请求

[英]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.

相关问题 webclient 和 expect100continue - webclient and expect100continue 当 Expect100Continue header 被禁用时,HttpWebRequest 会引入显着延迟 - HttpWebRequest introduces significant latency when the Expect100Continue header is disabled "如何为单个请求禁用 HttpWebRequest 中的“期望:100 继续”标头?" - How to disable the "Expect: 100 continue" header in HttpWebRequest for a single request? 如何在编码的Visual Studio 2013 WebTestRequest上将Expect100Continue设置为false? - How does one set Expect100Continue to false on a Coded Visual Studio 2013 WebTestRequest? 即使将Expect100Continue设置为false,Webclient UploadData也会返回错误417 - Webclient UploadData returns error 417 even after putting expect100Continue to false 正确处理期望失败(417),并在WCF客户端应用程序中更改Expect100Continue - Correcty handle Expectation failed (417) and change Expect100Continue in WCF client app C#Owin自我托管-预期标头100-继续 - C# Owin Self Host - Expected header 100-continue 期望:100-继续 - Expect: 100-continue 如何在WinRT的HttpWebRequest中禁用“Expect:100 continue”标头 - How to disable the “Expect: 100 continue” header in WinRT's HttpWebRequest foreach循环中的C#控制台应用程序HTTP API请求-如何在100个循环中暂停循环然后继续? - C# Console App HTTP API request in a foreach loop - how to pause loop at 100 loops then continue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM