I have a problem. I want to do a post action and I’m doing it like this:
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();
This is my code-behing of my 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));
}
}
And my Process method look like this:
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();
}
The problem is I get no parameters on my Deafault.aspx. When I’m doing it in my browser everything is ok. Do you know what can be a problem? Thanks in advance;)
Using the browser method, you are passing the variables by
GET(i.e., using parameters passed as part of the url). Using the code, you are passing byPOST. Those are two different things, and they are handled differently when processing on the server side.When you reference the variables in the code for Default.aspx, you are using
Request.QueryStringwhich looks specifically for parameters passed viaGET, or in the url. To retrieve the variables passed viaPOST, you need to useRequest.Form["varName"].