Home > Net >  This code is how the value passed to the controls.
This code is how the value passed to the controls.

Time:11-15

Class Program
{
The static HttpListener httpobj;
The static void Main (string [] args)
{
//provides a simple, can be programmatically control the HTTP protocol listener, such can't be inherited,
Httpobj=new HttpListener ();
//define the url and port number, usually set to the configuration file
Httpobj. Prefixes. Add (" http://+:8080/");
//start the listener
Httpobj. Start ();
//asynchronous monitor client request, when the client network request arrival automatically execute Result entrust
//the delegate has no return value, have an IAsyncResult interface parameters, can get the context object by the parameter
Httpobj. BeginGetContext (Result, null);
Console. WriteLine ($" server-side initialization is completed, is waiting for the client request, time: {DateTime. Now. The ToString ()} \ r \ n ");
Console.ReadKey();
}


Private static void Result (IAsyncResult ar)
{
//when receives the request process flows go here

//continue to asynchronous listening
Httpobj. BeginGetContext (Result, null);
Var guid=guid. NewGuid (). The ToString ();
The Console. ForegroundColor=ConsoleColor. White;
Console. WriteLine ($" received a new request: {guid}, time: {DateTime. Now. The ToString ()} ");
//get the context object
Var context=httpobj. EndGetContext (ar);
Var request=context. The request;
Var response=context. The response;
////if the js ajax requests, you can also set the IP address of the cross-domain and parameter
//the context. The Response. AppendHeader (" Access - Control - Allow - Origin ", "*");//the background cross-domain request, usually set to the configuration file
//context. The Response. AppendHeader (" Access - Control - Allow - Headers ", "ID, PW");//the background cross-domain parameter Settings, usually set to the configuration file
//the context. The Response. AppendHeader (" Access - Control - Allow - Method ", "post");//background cross-domain request set, usually set to the configuration file
The context. The Response. ContentType="text/plain; Charset=utf-8 ";//tell the client return the ContentType type is pure text format, encoding to utf-8
Context. The Response. AddHeader (" the content-type ", "text/plain");//add response header information
The context. The Response. ContentEncoding=Encoding. UTF8;
String returnObj=null;//define returned to the client's information
If (request. HttpMethod=="POST" & amp; & Request InputStream!=null)
{
//handle the client sends the request and returns processing information
ReturnObj=HandleRequest (request, response);
}
The else
{
ReturnObj=$" is not a post request to get their data is null or ";
}
Var returnByteArr=Encoding. UTF8. GetBytes (returnObj);//set the client returns the information encoding
Try
{
Using (var stream=response OutputStream)
{
//to process information back to the client
Stream. Write (returnByteArr, 0, returnByteArr. Length);
}
}
The catch (Exception ex)
{
The Console. ForegroundColor=ConsoleColor. Red;
Console. WriteLine ($" network jump: {ex. ToString ()} ");
}
The Console. ForegroundColor=ConsoleColor. Yellow;
Console. WriteLine ($" request processing is completed: {guid}, time: {DateTime. Now. The ToString ()} \ r \ n ");
}

Private static string HandleRequest (HttpListenerRequest request, HttpListenerResponse response)
{
String data=https://bbs.csdn.net/topics/null;
Try
{
Var byteList=new List (a);
Var byteArr=new byte [2048];
Int readLen=0;
int len=0;
Pass//receive the client data and converted to string type
Do
{
ReadLen=request. InputStream. Read (byteArr, 0, byteArr. Length);
Len +=readLen;
ByteList. AddRange (byteArr);
} while (readLen!=0);
Data=https://bbs.csdn.net/topics/Encoding.UTF8.GetString (byteList ToArray (), 0, len);

//data access to get data can perform other operations
}
The catch (Exception ex)
{
The response. StatusDescription="404";
The response. The StatusCode=404;
The Console. ForegroundColor=ConsoleColor. Red;
Console. WriteLine ($" there was an error in receiving data: {ex. ToString ()} ");
Return $" there was an error in receiving data: {ex. ToString ()} ";//the server error information returned directly may result in unsafe, here are for reference only
}
The response. StatusDescription="200";//get or set the HTTP status code returned to the client's text descriptions,
The response. The StatusCode=200;//get or set the HTTP status code returned to the client,
The Console. ForegroundColor=ConsoleColor. Green;
Console. WriteLine ($" receiving data complete: {data. The Trim ()}, time: {DateTime. Now. The ToString ()} ");
Return $" receiving data to complete ";
}
}



This code is to create a simple HTTP service, problem is asynchronous in return data, how I returned to the controls,,
Controls to use Async keyword?

CodePudding user response:

"Stick to the points, I myself, I define a public event has passed,

CodePudding user response:

Can also give yourself points,

CodePudding user response:

Had to yourself to open small to take
  •  Tags:  
  • C#
  • Related