开发者

How to customize tcp port number other than default (80)

开发者 https://www.devze.com 2022-12-31 04:58 出处:网络
I\'m using a web servi开发者_如何学Goce and connect it using httpWebRequest.create api. If i change the TCP port number In IIS other than 80 then my application could not connect to it. How can i set

I'm using a web servi开发者_如何学Goce and connect it using httpWebRequest.create api. If i change the TCP port number In IIS other than 80 then my application could not connect to it. How can i set port number in System.Url object as set in IIS so that my application can connect to web service.


You'd normally do this by appending the port like so:

http://www.example.com:81/path/to/page


use URI in form http://example.com:8080/ where 8080 can be any other


I think, if the Uri of your webservice is http://webservice/ then you might just do http://webservice:1234 where 1234 is your new port..


  • Using WebRequest.Create with string parameter :

    WebRequest.Create("http://{server}:{port});
    
  • Using WebRequest.Create with uri parameter :

    Uri myUri = new Uri("http://{server}:{port}");
    WebRequest.Create(Uri);
    


Determining the port of an IIS that runs on a remote machine is not easy. Either you will need to have a different way of communicating the configuration (like a service), or use a portscanner that will check all possible ports (not recommended).

but, if the IIS is running on the local machine, you can use the appcmd command to get a list of sites running in IIS.

appcmd list site

If you want to do this programmatically in C#, you could do something along the lines of:

// Setup ProcessStartInfo
var processInfo = new ProcessStartInfo();
processInfo.FileName = Environment.ExpandEnvironmentVariables("%windir%\system32\inetsrv\appcmd.exe");
processInfo.Arguments = "list site";
processInfo.RedirectStandardOutput = true;
processInfo.UseShellExecute = false;

// Start the process
var process = new Process();
process.StartInfo = processInfo; 
process.Start(processInfo);

// Capture the output and wait for exit
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();

// Parse the output
var ports = Regex.Matches(output, ":([0-9]+):");
foreach (Match port in ports)
{
    // TODO: Do something with the ports here
    Console.WriteLine(port.Groups[1].Captures[0].Value);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消