我遇到問題。我希望做一個動作後,我做這樣的: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參數。當我在瀏覽器中進行操作時,一切正常。你知道什麼是問題嗎?預先感謝;)
當你在瀏覽器中測試時,你是通過'GET'還是'POST'傳遞它們? 'Default.aspx'中的代碼是否在'GET','POST'或兩者中尋找變量? – mellamokb
在瀏覽器中,我只需輸入如下的文本:http:// localhost:60837/default.aspx?taskId = 3&value = 10&inputId = 44 –
使用瀏覽器方法,您通過'GET'傳遞變量。使用代碼,你通過'POST'傳遞。當他們查看'Request.QueryString [..]和Request.Form [..]'時,它們的引用是不同的(除非你使用的是不鼓勵的catch-all方法)。你如何檢索'Default.aspx'中的變量?這可能會有所作爲。 – mellamokb