[英]POST action - no parameters
我有個問題。 我想進行后期處理,我正在這樣做:
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的代碼格式:
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));
}
}
我的Process方法看起來像這樣:靜態只讀字符串// 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沒有參數。 當我在瀏覽器中執行此操作時,一切正常。 您知道什么可能是問題嗎? 提前致謝;)
使用瀏覽器方法,您正在通過GET
傳遞變量(即,使用作為URL一部分傳遞的參數)。 使用代碼,您將通過POST
傳遞。 這是兩件事,在服務器端進行處理時,它們的處理方式也不同。
當您在Default.aspx的代碼中引用變量時,您使用的是Request.QueryString
,它專門查找通過GET
或url傳遞的參數。 要檢索通過POST
傳遞的變量,您需要使用Request.Form["varName"]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.