Arvind Bhatt
Arvind Bhatt

Reputation: 157

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.

Upvotes: 2

Views: 21801

Answers (5)

Wazner
Wazner

Reputation: 3102

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

Upvotes: 0

Julien Hoarau
Julien Hoarau

Reputation: 49970

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

Upvotes: 0

Salil
Salil

Reputation: 88

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..

Upvotes: 0

Andrey
Andrey

Reputation: 60065

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

Upvotes: 4

David M
David M

Reputation: 72860

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

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

Upvotes: 4

Related Questions