Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Expect100Continue标头请求_C#_Http_Httpwebrequest_Webclient_Httpcookie - Fatal编程技术网

C#Expect100Continue标头请求

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

我面临着在不同域中发布用户名和密码的问题-一个域成功提交表单,而另一个域未成功提交表单(表单数据为空)!两个域上的html代码相同。下面是示例代码-评论的域没有发布:非常感谢任何帮助

注意:在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);

问题在于,每次通过程序发出请求时,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命令时得到的响应