C # httplistener error if remote ip
I created an httplistener and only works with localhost as prefix. It shows error if I change it to the IP address of the remote server.
Here's the code:
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://*:8080/"); // I need localhost replaced with remote ip
listener.Start();
while (true)
{
// Wait for a client request:
HttpListenerContext context = listener.GetContext();
// Respond to the request:
string msg = "You asked for: " + context.Request.RawUrl;
context.Response.ContentLength64 = Encoding.UTF8.GetByteCount(msg);
context.Response.StatusCode = (int)HttpStatusCode.OK;
using (Stream s = context.Response.OutputStream)
using (StreamWriter writer = new StreamWriter(s))
writer.Write(msg);
}
listener.Stop();
This is the web client:
using (WebClient wc = new WebClient()) // Make a client request.
wc.DownloadString
("http://"my public ip"(on my router):8080/lg.txt"); //port forwarding is set
MessageBox.Show("received");
It only works if I change "my public ip" to my local ip Any ideas?
+3
source to share
2 answers
First, check your firewall. This is probably most likely http.sys, which is used by the HttpListener - you must make the prefix available to the account via "netsh". You can verify this by running as an elevated administrator account in a nutshell - if that works, it might be a http.sys permissions issue.
+1
source to share