I want to use ASP to generate code in a C # desktop application.
To do this, I set up a simple host (obtained from System.MarshalByRefObject) that processes System.Web.Hosting.SimpleWorkerRequest through HttpRuntime.ProcessRequest. This processes the ASPX script specified by the incoming request (using System.Net.HttpListener to wait for requests).
The client side is represented by System.ComponentModel.BackgroundWorker, which builds System.Net.HttpWebRequest and receives a response from the server.
A simplified version of my client code is as follows:
private void SendRequest (object sender, DoWorkEventArgs e)
{
// create request with GET parameter
var uri = "http: // localhost: 9876 / test.aspx? getTest = 321";
var request = (HttpWebRequest) WebRequest.Create (uri);
// append POST parameter
request.Method = "POST";
request.ContentType = "application / x-www-form-urlencoded";
var postData = Encoding.Default.GetBytes ("postTest = 654");
var postDataStream = request.GetRequestStream ();
postDataStream.Write (postData, 0, postData.Length);
// send request, wait for response and store / print content
using (var response = (HttpWebResponse) request.GetResponse ())
{
using (var reader = new StreamReader (response.GetResponseStream (), Encoding.UTF8))
{
_processsedContent = reader.ReadToEnd ();
Debug.Print (_processsedContent);
}
}
}
My server part code looks like this (without exception handling, etc.):
public void ProcessRequests ()
{
// HttpListener at http://localhost:9876/
var listener = SetupListener();
// SimpleHost created by ApplicationHost.CreateApplicationHost
var host = SetupHost();
while (_running)
{
var context = listener.GetContext();
using (var writer = new StreamWriter(context.Response.OutputStream))
{
// process ASP script and send response back to client
host.ProcessRequest(GetPage(context), GetQuery(context), writer);
}
context.Response.Close();
}
}
, GET. POST- ASPX script, . script:
// GET parameters are working:
var getTest = Request.QueryString["getTest"];
Response.Write("getTest: " + getTest); // prints "getTest: 321"
// don't know how to access POST parameters:
var postTest1 = Request.Form["postTest"]; // Request.Form is empty?!
Response.Write("postTest1: " + postTest1); // so this prints "postTest1: "
var postTest2 = Request.Params["postTest"]; // Request.Params is empty?!
Response.Write("postTest2: " + postTest2); // so this prints "postTest2: "
, System.Web.HttpRequest, ASP, POST "postTest". , "postTest", "654". BinaryRead Request, , , . Request.InputStream == null Request.ContentLength == 0. - , Request.HttpMethod "GET"?!
, PHP script ASPX script. :
print_r($_GET); // prints all GET variables
print_r($_POST); // prints all POST variables
:
Array
(
[getTest] => 321
)
Array
(
[postTest] => 654
)
PHP script , POST. ASPX script ? ? Response ?
- , ? .