简体   繁体   中英

.Net HttpListener: how to identify calling process

I've written a proxy server using System.Net.HttpListener and C# (Windows 7+, .Net 4.0). I would like to identify which process is sending the request (the sender is guaranteed to be on the same machine).

Good old @EricLaw has something that's almost works, http://blogs.msdn.com/b/fiddler/archive/2013/01/09/10138573.aspx but... only if I subtract 1 from the Port #:

HttpListenerContext context = GetContext();
HttpListenerRequest request = context.Request;
int pid = 0;
if (request.IsLocal)
{
    int port = context.Request.RemoteEndPoint.Port;
    pid = Winsock.MapLocalPortToProcessId(port - 1)
}
return pid;

If I don't subtract 1 the request originates from PID=4, the System process.

Is the subtraction the thing I should be doing? Or is there a different way to determine which process sent the request?

The code shared on my blog works just fine; it's used in Fiddler, which is used by millions of people.

You should dump the entire port list and see what's going on; perhaps there's a problem in the value returned from context.Request.RemoteEndPoint.Port .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM