1

我有个问题。我想做一个发布操作,我这样做是这样的:

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 方法如下所示: 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 上没有任何参数。当我在浏览器中执行此操作时,一切正常。你知道可能是什么问题吗?提前致谢;)

4

1 回答 1

1

使用浏览器方法,您正在传递变量GET(即,使用作为 url 的一部分传递的参数)。使用代码,您正在路过POST. 这是两个不同的东西,在服务器端处理时它们的处理方式不同。

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

于 2012-11-12T19:48:54.673 回答