using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.ServiceRuntime;
// var Params = new Dictionary<string, string>(); Params["key"] = "key_value"; // POST- string STDIN = ""; foreach (var Item in Params) { STDIN += Item.Key + "=" + HttpUtility.UrlEncode(Item.Value, Encoding.UTF8) + "&"; } byte[] sentData = Encoding.UTF8.GetBytes(STDIN); foreach (RoleInstance roleInst in RoleEnvironment.Roles["WebRole1"].Instances) { // WebRequest WebRequest reqPOST = WebRequest.Create( "http://" + roleInst.InstanceEndpoints["InterEndpoint"].IPEndpoint + "/Communicator.ashx"); reqPOST.Method = "POST"; reqPOST.Timeout = 120000; reqPOST.ContentType = "application/x-www-form-urlencoded"; // reqPOST.ContentLength = sentData.Length; System.IO.Stream sendStream = reqPOST.GetRequestStream(); sendStream.Write(sentData, 0, sentData.Length); sendStream.Close(); System.Net.WebResponse resp = reqPOST.GetResponse(); System.IO.Stream stream = resp.GetResponseStream(); System.IO.StreamReader sr = new StreamReader(stream); string s = sr.ReadToEnd(); }
// if (context.Request.Form["key"] != "key_value") return; //
private HttpListener listener = null; private AutoResetEvent connectionWaitHandle = new AutoResetEvent(false); public override void Run() { // HttpListener Thread HttpListenerThread = null; while (true) { if (HttpListenerThread == null) { HttpListenerThread = new Thread(new ThreadStart(HttpListenerHandler)); HttpListenerThread.IsBackground = true; HttpListenerThread.Start(); } Thread.Sleep(1000); } } protected void HttpListenerHandler() { // HttpListener ServiceEndpoint if (listener == null) { listener = new HttpListener(); var HostEndpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["ServiceEndpoint"].IPEndpoint.ToString(); listener.Prefixes.Add(string.Format("http://{0}/", HostEndpoint)); listener.Start(); } while (true) { IAsyncResult result = listener.BeginGetContext(HandleAsyncConnection, listener); connectionWaitHandle.WaitOne(); } } private void HandleAsyncConnection(IAsyncResult result) { // POST- HttpListener listener = (HttpListener)result.AsyncState; HttpListenerContext context = listener.EndGetContext(result); connectionWaitHandle.Set(); var Request = context.Request; var Response = context.Response; if (Request.HttpMethod == "POST") { Stream BodyStream = context.Request.InputStream; var encoding = context.Request.ContentEncoding; var reader = new StreamReader(BodyStream, encoding); var PostParams = HttpUtility.ParseQueryString(reader.ReadToEnd(), encoding); if (PostParams["key"] != "key_value") return; // } Response.OutputStream.Close(); }
Source: https://habr.com/ru/post/140461/
All Articles