2012-11-20 44 views
0

我面臨的問題與不同的域發佈的用戶名和密碼 - 一個成功的提交形式,而其他沒有(表格數據是空的)!兩個域的html代碼是相同的。這裏是示例代碼 - 評論域不發佈:任何幫助非常感謝!C#Expect100Continue頭請求

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

這個計算器後看看http://stackoverflow.com/questions/5401501/how-to-post-data-to-specific-url-using-webclient-in-c-sharp – MethodMan

+0

燦您嘗試將請求/響應與代理HTTP監視工具(如[Fiddler](http://www.fiddler2.com/))進行比較? – mellamokb

+0

從響應頭文件中,我看到Apache響應中缺少了幾個頭文件。這會是一個問題嗎? 1.連接 2.傳輸編碼 從nginx的服務器的響應是: 連接:保活和傳輸編碼 :分塊 這些都是我通過我的C#程序得到答覆,當我運行curl命令 – Srinivas

回答

1

的問題是與自動添加Expect100Continue頭域每次通過Apache上沒有很好處理的程序發出請求時。每次以下列方式發出請求時,您都必須將Expect100Continue設置爲false。儘管我可以通過Amazon EC2實例上的dumpcap工具查看,但感謝Fiddler領導!這是解決方案!

# 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);