POST动作 - 没有参数

时间:2012-11-12 19:39:55

标签: c# asp.net http-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:

的代码
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 =“定义”,             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没有参数。当我在浏览器中执行此操作时,一切正常。你知道什么是问题吗?在此先感谢;)

1 个答案:

答案 0 :(得分:1)

使用浏览器方法,您将通过GET传递变量(即,使用作为URL的一部分传递的参数)。使用代码,您将经过POST。这些是两个不同的东西,在服务器端进行处理时处理方式不同。

当您在Default.aspx的代码中引用变量时,您使用的是Request.QueryString,它专门查找通过GET或网址传递的参数。要检索通过POST传递的变量,您需要使用Request.Form["varName"]

相关问题