2012-11-03 218 views
0

Http post返回錯誤400錯誤的請求,這是我的代碼,你能看到我要去哪裏錯了嗎?我猜這是與URL有關?Http post返回錯誤400錯誤的請求asp.net c#

System.Collections.Specialized.NameValueCollection data = new System.Collections.Specialized.NameValueCollection(); 
     data["var1"] = source1; 
     data["var2"] = source2; 
     data["var3"] = source3; 


    // HTTP post and redirect the values 
    string str2 = string.Empty; 

    System.Text.StringBuilder builder = new System.Text.StringBuilder(); 

    foreach (string str3 in data.AllKeys) 
    { 
     builder.Append(str2); 
     builder.Append(HttpUtility.UrlEncode(str3)); 
     builder.Append("="); 
     builder.Append(HttpUtility.UrlEncode(data[str3])); 
     str2 = "&"; 
    } 

    //Make a request 
    string path = Request.Url.AbsoluteUri; 
    string url = path + Target + builder; 
    url = "http://localhost:1541/test/var1=SELEZIONA&var2=SELEZIONA&var3=SELEZIONA"; 
    WebRequest request = WebRequest.Create(url); 
    request.Method = "POST"; 
    request.ContentType = "application/x-www-form-urlencoded"; 

    //Put the post data into the request 
    byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(builder.ToString()); 

    request.ContentLength = byteArray.Length; 
    Stream reqStream = request.GetRequestStream(); 
    reqStream.Write(byteArray, 0, byteArray.Length); 
    reqStream.Close(); 

    //Get response 
    System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse(); 
    response.Close(); 

我已經把url中的實際url內容放到url變量中來顯示它當前正試圖發送的內容。

+0

愚蠢的問題,是網站運行和應用程序池開始? –

回答

0

您在URL中不存在用於分隔查詢字符串的問號。我想這可能會導致這種錯誤。試試這個:

url = "http://localhost:1541/test/?var1=SELEZIONA&var2=SELEZIONA&var3=SELEZIONA"; 
+0

啊當然錯過了。在運行http post之後,頁面是否應該重定向到目標?它似乎還沒有做任何事情。 – PeteTheGreek

+0

@PeteTheGreek嗯,你告訴我它應該做什麼。該代碼只讀取請求的響應。你接下來做的是你。 –

+0

我試圖讓我的腦袋從後面的代碼發佈到另一個頁面,這個代碼發送http數據還是接收?謝謝,皮特。 – PeteTheGreek