2012-11-12 30 views
1

我遇到問題。我希望做一個動作後,我做這樣的:POST操作 - 無參數

string post_data = string.Format("taskId={0}&inputId={1}&value={2}", taskId, inputId, "101"); 

string uri = "http://localhost:60837/Default.aspx"; 
// Create a request using a URL that can receive a post. 
     WebRequest request = WebRequest.Create(uri); 
     // Set the Method property of the request to POST. 
     request.Method = "POST"; 
     // Create POST data and convert it to a byte array 
     var byteArray = Encoding.UTF8.GetBytes(post_data); 
     // Set the ContentType property of the WebRequest. 
     request.ContentType = "application/x-www-form-urlencoded"; 
     // Set the ContentLength property of the WebRequest. 
     request.ContentLength = byteArray.Length; 
     // Get the request stream. 
     Stream dataStream = request.GetRequestStream(); 
     // Write the data to the request stream. 
     dataStream.Write(byteArray, 0, byteArray.Length); 
     // Close the Stream object. 
     dataStream.Close(); 
     // Get the response. 
     WebResponse response = request.GetResponse(); 
     // Display the status. 
     Console.WriteLine(((HttpWebResponse)response).StatusDescription); 
     // Get the stream containing content returned by the server. 
     dataStream = response.GetResponseStream(); 
     // Open the stream using a StreamReader for easy access. 
     StreamReader reader = new StreamReader(dataStream); 
     // Read the content. 
     string responseFromServer = reader.ReadToEnd(); 
     // Display the content. 
     Console.WriteLine(responseFromServer); 
     // Clean up the streams. 
     reader.Close(); 
     dataStream.Close(); 
     response.Close(); 

這是我的Default.aspx我的代碼behing:

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     Response.Write(Caller.Process(Request.RawUrl, Request.QueryString, Request.UserHostAddress)); 
    } 
} 

我的加工方法是這樣的: static static readonly string // paramDefinition =「definition」, paramTaskId =「taskId」, paramInputId =「inputId」, paramValue =「value」;

static public string Process(string query, NameValueCollection collection, string ip) 
    { 
     StringBuilder result = new StringBuilder(); 

     Func<string, bool> check = str => 
     { 
      if (!collection.AllKeys.Contains(str)) 
      { 
       result.AppendLine(string.Format("No {0} parameter. ", str)); 
       return false; 
      } 
      return true; 
     }; 
     if (check(paramTaskId) && check(paramInputId) && check(paramValue)) 
     { 
      result.Append("OK"); 
      Execute(query, collection, ip); 
     } 
     else 
     { 
      WriteLog(result.ToString(), query, ip); 
     } 

     return result.ToString(); 
    } 

問題是我沒有得到我的Deafault.aspx參數。當我在瀏覽器中進行操作時,一切正常。你知道什麼是問題嗎?預先感謝;)

+0

當你在瀏覽器中測試時,你是通過'GET'還是'POST'傳遞它們? 'Default.aspx'中的代碼是否在'GET','POST'或兩者中尋找變量? – mellamokb

+0

在瀏覽器中,我只需輸入如下的文本:http:// localhost:60837/default.aspx?taskId = 3&value = 10&inputId = 44 –

+1

使用瀏覽器方法,您通過'GET'傳遞變量。使用代碼,你通過'POST'傳遞。當他們查看'Request.QueryString [..]和Request.Form [..]'時,它們的引用是不同的(除非你使用的是不鼓勵的catch-all方法)。你如何檢索'Default.aspx'中的變量?這可能會有所作爲。 – mellamokb

回答

1

使用瀏覽器方法,您通過GET(即使用作爲url的一部分傳遞參數)傳遞變量。使用該代碼,您將通過POST。這些是兩個不同的東西,在服務器端處理時它們的處理方式不同。

當您在Default.aspx的代碼中引用變量時,您使用的是Request.QueryString,它專門用於查看通過GET傳遞的參數,或者在url中。要檢索通過POST傳遞的變量,您需要使用Request.Form["varName"]

+0

我發佈了我的Process方法。我想我是對的。 –

+0

是的,只是用'Request.Form'代替'Request.QueryString'。 – mellamokb

+0

非常感謝你:) 6小時後有時很難想:) –