• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

HttpListener Issue - works on my machine but not on another server

I have hosted a web service on an Https port on my local machine, when the service gets a request from the client it sends back an "ACK" signal. Please see below my code in c# :
 
static void Main(string[] args)
{      try{
            System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
            listener.Prefixes.Add("https://localhost/");
            listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
            listener.Start();
      }catch (HttpListenerException exception)
      {
            Console.WriteLine(exception);
      }
      while (listener.IsListening) //For ProcessRequestwithContext()
      {
            var context = listener.GetContext();
            ProcessRequestwithContext(context);
      }
      listener.Close();
}
private static void ProcessRequestwithContext(HttpListenerContext context)
{
      Console.WriteLine("Request received through ProcessRequestwithContext");
      var body = new StreamReader(context.Request.InputStream).ReadToEnd();
      byte[] b = Encoding.UTF8.GetBytes("ACK");
      context.Response.StatusCode = 200;
      context.Response.KeepAlive = false;
      context.Response.ContentLength64 = b.Length;
      var output = context.Response.OutputStream;
      output.Write(b, 0, b.Length);
      Console.WriteLine(output.ToString());
      context.Response.Close();
}


My problem is that the above code works perfectly on my local machine but when I try to listen on an https port of another machine as follows, I don't get an "ACK" back :
listener.Prefixes.Add("https://abc");

Please tell me what I'm missing ? I'm unable to debug this issue. My intention is to setup a simple web service that can listen continuously to Post requests from the client and service them accordingly.
0
oldstone00
Asked:
oldstone00
  • 2
1 Solution
 
Bob LearnedCommented:
I am curious about this approach, versus something like Web API, which can still be a simple web service...
0
 
oldstone00Author Commented:
Hi,

My goal is to accept Http POST requests from a client and process them in c#. I have exposed a url endpoint to a client who will send me Post requests. My aim is to catch and decode the JSON data in those POST requests using c#.

I'm not sure if what I'm doing is the best way so please suggest what my options are ?

Thanks in advance!
0
 
Bob LearnedCommented:
Personally, I prefer the options that come with a dedicated web service, such as WebAPI, but that's me.

Building ASP.Net Web API RESTful Service – Part 5
http://bitoftech.net/2013/11/25/implement-http-actions-post-put-delete-web-api/

I like that idea better than using a while loop to wait for requests:

while (listener.IsListening) 
      {
            var context = listener.GetContext();
            ProcessRequestwithContext(context);
      }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now