locked
Trying to get a simple "Hello World" response from ASHX "page" RRS feed

  • Question

  • User1950603156 posted

    First of all, I apologize if this is not in the right subforum.

    I have created a basic API that allows a user to click a link that creates an account on our client's website.  Currently, the customer is logged in and brought to the customer's profile page if the creation is successful, otherwise the customer is brought to an error page.  Our client would like to change the customer creation method so that instead of the customer creating a link, our client simply makes calls to our server and receives a response in an XML document.  

    I have created a simple ASHX (CreateCustomer.ashx) page with the following code:

    namespace MyNamespace
    {
        /// <summary>
        /// Summary description for $codebehindclassname$
        /// </summary>
        public class CreateCustomer : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.Clear();
                context.Response.ContentType = "text/plain";
                context.Response.Write("Hello World");
                context.Response.End();
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }



    For now, I simply want to see if I can even get the "Hello World" response. 

    In my ASPX page (https://www.mywebsite.com/APITestCaller.aspx), I make an HTTP Web Request to "https://www.mywebsite.com/CreateCustomer.ashx" using an HTTP Wrapper (a programmer who used to work at our company wrote this wrapper for something else):

            public static HTTPResponse SendToHTTP(string URL, string Method, string Message, string ContentType)
            {
                HTTPResponse retval = new HTTPResponse();
                byte[] Bytes = Encoding.ASCII.GetBytes(Message);
    
    
                retval.RequestURL = URL + Message;
    
                try
                {
                    HttpWebRequest HTTP = (HttpWebRequest)WebRequest.Create(URL);
    
                    HTTP.Method = Method;
    
                    if (ContentType != "")
                    {
                        HTTP.ContentType = ContentType;
                    }
    
                    if (Method.ToUpper() == "POST")
                    {
                        HTTP.ContentLength = Bytes.Length;
    
                        Stream os = HTTP.GetRequestStream();
    
                        os.Write(Bytes, 0, Bytes.Length);
    
                        os.Close();
                    }
    
                    HttpWebResponse Resp = (HttpWebResponse)HTTP.GetResponse();
    
                    if (Resp == null)
                    {
                        retval.ResponseCode = -1;
                        retval.Body = "No response";
                    }
                    else
                    {
                        StreamReader sr = new StreamReader(Resp.GetResponseStream());
                        retval.ResponseCode = (int)Resp.StatusCode;
                        retval.ResponseMsg = Resp.StatusCode.ToString();
                        retval.Body = sr.ReadToEnd().Trim();
                    }
                }
                catch (Exception e)
                {
                    retval.ResponseCode = -1;
                    retval.Body = e.Message;
                }
    
                return retval;
            }

    The response message I get is "Unable to connect to the remote server" with a response code of "-1" (which is obviously due to request code).  Keep in mind that ultimately my code for the request won't matter because our client will be in charge of the request, but I included it in case that is what is causing the error message.

    Is this an IIS issue? Or is my coding for the response bad?  Anything else it could be?

    Thank you!

    Saturday, April 13, 2013 4:46 PM

Answers

All replies

  • User1508394307 posted

    To make a web request to a remote URL via HTTPS protocol, it’s required more works than via HTTP because it needs to validate the SSL Certificate. Here's some hint. Hope it helps.

    Saturday, April 13, 2013 5:18 PM
  • User1950603156 posted

    I know that my request code works with HTTPS already because I've used it before on another URL that had HTTPS. 

    Saturday, April 13, 2013 5:31 PM
  • User1508394307 posted

    from the same domain/certificate where CreateCustomer.ashx is?

    Saturday, April 13, 2013 5:49 PM
  • User-1137493631 posted

    This should be problem with your certificate, is that certificates configured in your server properly ? 

    Saturday, April 13, 2013 9:45 PM
  • User1950603156 posted

    I took a look at the example... should I be able to get a response just by accepting any certificate like the example shows?  I am still getting the same response.

    Monday, April 15, 2013 2:19 PM
  • User1950603156 posted

    Another question I have is, may I make a request directly to https://www.mywebsite.com/CreateCustomer.ashx or is the ashx just a handler for an aspx page?

    Monday, April 15, 2013 2:27 PM
  • User1950603156 posted
    Please help if you can.
    Tuesday, April 16, 2013 1:04 PM
  • User-1241139641 posted

    To answer to the last question you posted, a HttpHandler can be called directly in the browser like http://mysite/handler.ashx. You can think of a handler it like calling a regular aspx page except you control the output directly.

    Here's some more info if you need it - Custom ASP.NET procesing for HttpHandlers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 16, 2013 2:08 PM