locked
How to call WebMethod of a Web service using HttpWebRequest object RRS feed

  • Question

  • User-1874132285 posted

    I have a webservice with just one webmethod that sends email. I want to call webservice using HttpWebrequest object. Below is my Code

    HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create("https://www.stratamanageit.com.au/FileUploadServiceMailProcess.asmx/SendServiceMail");
    myRequest.Method = "POST";
    myRequest.ContentLength = 0;
    CookieContainer ck = new CookieContainer();
    ck.Add(new Cookie("TemplateID", "4236", "/", myRequest.RequestUri.Host));
    ck.Add(new Cookie("WorkOrderID", "12914", "/", myRequest.RequestUri.Host));
    ck.Add(new Cookie("SchemeNumber", "000001", "/", myRequest.RequestUri.Host));
    ck.Add(new Cookie("UserID", "0", "/", myRequest.RequestUri.Host));
    myRequest.CookieContainer = ck;
    
    HttpWebResponse response = myRequest.GetResponse() as HttpWebResponse;
    StreamReader reader = new StreamReader(response.GetResponseStream());
    Response.Write(reader.ReadToEnd());
    


    I am using cookie container to store my data and those cookies are fetched in different class called by WebMethod and emails are send. In my local server everything is working fine but when I deployed to production, things are strange because no mail and no log are recorded. I created a test website to send mail directly by calling webservice method and i am getting Internal server error : 500.
    Do you have any idea? What it should be ?

    Wednesday, May 2, 2012 1:05 AM

All replies

  • User-1874132285 posted

    Hi, I tried your first link but no luck its gives no error and not sending mails. See my Code.

     string soap =
                @"<?xml version=""1.0"" encoding=""utf-8""?>
    <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
       xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
       xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
      <soap:Body>
        <SendServiceMail xmlns=""http://tempuri.org/"">
        </SendServiceMail>
      </soap:Body>
    </soap:Envelope>";
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create("https://www.stratamanageit.com.au/FileUploadServiceMailProcess.asmx");
                myRequest.Headers.Add("SOAPAction", "\"http://tempuri.org/SendServiceMail\"");
                myRequest.ContentType = "\"text/xml; charset=\"utf-8\"";
                myRequest.Accept = "text/xml";
                myRequest.Method = "POST";
    
                using (Stream stm = myRequest.GetRequestStream())
                {
                    using (StreamWriter stmw = new StreamWriter(stm))
                    {
                        stmw.Write(soap);
                    }
                }
    
    
                CookieContainer ck = new CookieContainer();
                ck.Add(new Cookie("TemplateID", "4236", "/", myRequest.RequestUri.Host));
                ck.Add(new Cookie("WorkOrderID", "12914", "/", myRequest.RequestUri.Host));
                ck.Add(new Cookie("SchemeNumber", "000001", "/", myRequest.RequestUri.Host));
                ck.Add(new Cookie("UserID", "0", "/", myRequest.RequestUri.Host));
                myRequest.CookieContainer = ck;
    
                HttpWebResponse response = myRequest.GetResponse() as HttpWebResponse;
                StreamReader reader = new StreamReader(response.GetResponseStream());
                Response.Write(reader.ReadToEnd());
                reader.Close();



    Wednesday, May 2, 2012 1:25 AM
  • User-1874132285 posted

     

    Well thanks your reply indeed helped me, though I twisted the code to send data though Cookies. Here is my code.

                System.Net.ServicePointManager.ServerCertificateValidationCallback =
                  ((ser, certificate, chain, sslPolicyErrors) => true);
                string soap =
                @"<?xml version=""1.0"" encoding=""utf-8""?>
                    <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
                    xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
                    xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
                    <soap:Body>
                        <SendServiceMail xmlns=""http://tempuri.org/""/>
                    </soap:Body>
                    </soap:Envelope>";
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create("https://www.stratamanageit.com.au/FileUploadServiceMailProcess.asmx");
                myRequest.Headers.Add("SOAPAction", "\"http://tempuri.org/SendServiceMail\"");
                myRequest.ContentType = "text/xml;charset=\"utf-8\"";
                myRequest.Method = "POST";
    
                using (Stream stm = myRequest.GetRequestStream())
                {
                    using (StreamWriter stmw = new StreamWriter(stm))
                    {
                        stmw.Write(soap);
                    }
                }
    
                myRequest.Headers["Cookie"] = "TemplateID=4236;WorkOrderID=12914;SchemeNumber=000001;UserID=0";
    
                HttpWebResponse response = myRequest.GetResponse() as HttpWebResponse;
                StreamReader reader = new StreamReader(response.GetResponseStream());
                Response.Write(reader.ReadToEnd());
                reader.Close();

    For bypassing the server certificate validation I used this code

    System.Net.ServicePointManager.ServerCertificateValidationCallback = ((ser, certificate, chain, sslPolicyErrors) => true);

    It just need a callback delegate that'll always return true.


    Wednesday, May 2, 2012 5:32 AM
  • User1383283303 posted

    Hi , I am facing a problem
    In the following code you used
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(""https://www.stratamanageit.com.au/FileUploadServiceMailProcess.asmx");

    If I pass any valid URi inside webrequest.create it returns me HTTP 200 to irrespective of that web service contains that particular web method or not ( webmethod SendServiceMail in this case).
    I tried sending a URi of google it stil returns me HTTP 200.


    How to resolve this .. How can i hit my particular webmethod in my webservice.

     

    Tuesday, March 19, 2013 1:47 AM
  • User1383283303 posted

    Hi , I am facing a problem
    In the following code you used
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(""https://www.stratamanageit.com.au/FileUploadServiceMailProcess.asmx");

    If I pass any valid URi inside webrequest.create it returns me HTTP 200 to irrespective of that web service contains that particular web method or not ( webmethod SendServiceMail in this case).
    I tried sending a URi of google it stil returns me HTTP 200.


    How to resolve this .. How can i hit my particular webmethod in my webservice.

     

    Tuesday, March 19, 2013 1:47 AM