我有个问题。我想做一个发布操作,我这样做是这样的:
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 上没有任何参数。当我在浏览器中执行此操作时,一切正常。你知道可能是什么问题吗?提前致谢;)