简体   繁体   English

如何自定义默认值以外的tcp端口号(80)

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

I'm using a web service and connect it using httpWebRequest.create api. 我正在使用Web服务并使用httpWebRequest.create api连接它。 If i change the TCP port number In IIS other than 80 then my application could not connect to it. 如果我在80以外的IIS中更改TCP端口号,那么我的应用程序无法连接到它。 How can i set port number in System.Url object as set in IIS so that my application can connect to web service. 如何在IIS中设置的System.Url对象中设置端口号,以便我的应用程序可以连接到Web服务。

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

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

     WebRequest.Create("http://{server}:{port}); 
  • Using WebRequest.Create with uri parameter : 使用带有uri参数的 WebRequest.Create

     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. 确定在远程计算机上运行的IIS的端口并不容易。 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. 但是 ,如果IIS在本地计算机上运行,​​则可以使用appcmd命令获取在IIS中运行的站点列表。

appcmd list site

If you want to do this programmatically in C#, you could do something along the lines of: 如果您想在C#中以编程方式执行此操作,您可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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