简体   繁体   中英

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

I'm using a web service 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.

您通常通过附加端口来执行此操作:

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

使用表单http://example.com:8080/中的URI,其中8080​​可以是任何其他

我想,如果你的web服务的Uri是http:// webservice /那么你可能只需要http:// webservice:1234 ,其中1234是你的新端口..

  • 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);
}

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