locked
Access WebService hosted on IIS RRS feed

  • Question

  • Hi All,

    I have the following scenario,
    I've created a simple web service and managed to host it on my local IIS server running on my laptop.
    My laptop is assigned a static IP Address. e.g. 192.168.9.333 and my webservice is listening on port: 7001.

    I am able to access and invoke this webservice locally on my laptop. Meaning, I'm able to go to IE. and type in http://192.168.9.333:7001/MyWebService/ etc.

    Now, I have a requirement to make this webservice available publicly for testing purposes. In order to fulfilled this requirement, I have a public IP address that maps to the address and port of my laptop through NAT.

    Example. : Public IP : 58.123.123.1:7001 is mapped to Laptop static IP: 192.168.9.333:7001.

    However, I am not able to access my webservice from the public by going through 58.123.123.1:7001 and invoke it. I've disabled Firewall on my laptop. Hope I am making sense here.

    Can anyone offer any ideas as to what is wrong?

    Thanks in advance.


    jackerjack
    Monday, April 13, 2009 8:56 AM

Answers

  • I have three suggestions:

    1. Split the _httpRequest.GetRequestStream().Write(_requestData, 0, _requestData.Length) into two statements: Stream reqStream = _httpRequest.GetRequestStream(); reqStream.Write(_requestData, 0, _requestData.Length);
    2. You need to be placing using blocks around everything you create that implements IDisposable. That would include the request and response streams, and the HttpResponse as well.
    3. Your catch block should display the exception, then use "throw;". Do not throw a new exception.

    John Saunders
    Use File->New Project to create Web Service Projects
    Use WCF for All New Web Service Development, instead of old ASMX or obsolete WSE
    Wednesday, April 15, 2009 7:07 PM
    Moderator

All replies

  • This is a basic problem in network connectivity. Try to ping the public IP address. If you can ping it, try to use the telnet command to connect to it:

    telnet 58.123.123.1 7001

    Then carefully type "GET /" and press the Enter key. Note that these characters will not echo. Press Enter again if necessary. You may then see HTML output.

    John Saunders
    Use File->New Project to create Web Service Projects
    Use WCF for All New Web Service Development, instead of old ASMX or obsolete WSE
    Monday, April 13, 2009 12:15 PM
    Moderator
  • Can you access WebService from another PC in the local area network (192.168.9.x)? If not, then your problem is not NAT.

    By the way, what kind of IP 192.168.9.333 is? If it's IPv6, then I wouldn't be surprised that some routers/proxies/whatever have issues with it, or maybe more tweaking is required.

    Monday, April 13, 2009 1:50 PM
  • Hi All,
    Thanks for the help. I've managed to connect to the webservice from the public.

    However, I am now face with a very different problem.
    On the public network, I have my client app that access my webservice.

    I am making use of HTTP-POST to connect to the webservice. I have a simple method for this purpose.

    public string CallWebMethod(string webServiceURL, string webMethod, ArrayList dicParameters)
            {
                
                try
                {
                    Console.WriteLine("--- START ---");
                    byte[] _requestData = this.CreateHttpRequestData(dicParameters);

                    string uri = webServiceURL + "/" + webMethod;
                    HttpWebRequest _httpRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
                    Console.WriteLine("--- PREPARING ---");

                    _httpRequest.Method = "POST";
                    _httpRequest.KeepAlive = false;
                    _httpRequest.ContentType = "application/x-www-form-urlencoded";
                    _httpRequest.ContentLength = _requestData.Length;
                    _httpRequest.Timeout = 30000;
                    HttpWebResponse _httpResponse = null;
                    string _response = string.Empty;

                    Console.WriteLine("---  REQUEST ---");
                    _httpRequest.GetRequestStream().Write(_requestData, 0, _requestData.Length);

                    Console.WriteLine("--- RESPONSE ---");
                    _httpResponse = (HttpWebResponse)_httpRequest.GetResponse();
                    System.IO.Stream _baseStream = _httpResponse.GetResponseStream();
                    System.IO.StreamReader _responseStreamReader = new System.IO.StreamReader(_baseStream);
                    _response = _responseStreamReader.ReadToEnd();

                    Console.WriteLine("--- >> " + _response + " << ---");
                    _responseStreamReader.Close();

                    Console.WriteLine("--- END ---");
                    return _response;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    throw new Exception(ex.Message);
                }
            }


    using the above method, I am able to successfully invoke my webservice from the client app. and receive my response accordingly.
    However, Once I call this method from within an event handler, the code will always get stuck at

    Console.WriteLine("---  REQUEST ---");
    _httpRequest.GetRequestStream().Write(_requestData, 0, _requestData.Length);

    No exceptions are return, it will just hang till the request timeout.
    What is happening? I';ve tried many ways to work around this, like Closing the stream and even coding a separate thread to handle the method. But to no avail. The wierd thing, is , the same piece of code works fine if I simply call it from outside the event handler.

    1) This works fine

    ArrayList mylist = new ArrayList();
    CallWebMethod("http://mywebservice/service.asmx", "mymethod", mylist);


    2) This doesn;t work

    //--- declare a event handler ---
    ... += new MyClass.TestEventHandler(OnTestEvent);

    private void OnTestEvent()
    {
       ArrayList mylist = new ArrayList();
       CallWebMethod("http://mywebservice/service.asmx", "mymethod", mylist);

    }


    jackerjack
    Wednesday, April 15, 2009 8:29 AM
  • I've run your code, though I replaced

    byte[] _requestData = this.CreateHttpRequestData(dicParameters);

    with

    byte[] _requestData = new byte[50000];

    because I don't know what CreateHttpRequestData is.

     

    Also I've made CallWebMethod static.

     

    CallWebMethod ran fine from an event handler, when I wasn't going through it with debugger. When I was going through it with debugger I catch exception "The underlying connection was closed: An unexpected error occurred on a receive" at line

    _httpResponse = (HttpWebResponse )_httpRequest.GetResponse();

     

    So, I can't help but think that one must hurry when dealing with WebServices using "low level" functions. :)

    Wednesday, April 15, 2009 3:08 PM
  • I have three suggestions:

    1. Split the _httpRequest.GetRequestStream().Write(_requestData, 0, _requestData.Length) into two statements: Stream reqStream = _httpRequest.GetRequestStream(); reqStream.Write(_requestData, 0, _requestData.Length);
    2. You need to be placing using blocks around everything you create that implements IDisposable. That would include the request and response streams, and the HttpResponse as well.
    3. Your catch block should display the exception, then use "throw;". Do not throw a new exception.

    John Saunders
    Use File->New Project to create Web Service Projects
    Use WCF for All New Web Service Development, instead of old ASMX or obsolete WSE
    Wednesday, April 15, 2009 7:07 PM
    Moderator
  • Hi All,

    Thanks for the suggestion and help.

    I've since managed to solve this problem by simply making the call outside of the event handler.
    Wierd as it may seems, that has solved my problem.

    Appreciated all the help though. ;)
    jackerjack
    Saturday, April 25, 2009 5:18 PM
  • Hi John,

    Thanks for the suggestion.
    I've followed your suggestion and also put in the "using blocks".

    In addition, I've simply include the call outside the event handler which seems to have solve the problem. ;)
    jackerjack
    Saturday, April 25, 2009 5:19 PM