Thursday, January 23, 2014

Post data from client to a web service (.asmx, .svc) in C#

If you want to post data from your web application to web service then use the below line of code and change it according your requirement.

ClientSection clientSettings = ConfigurationManager.GetSection("system.serviceModel/client"as ClientSection;
                string address = string.Empty;
                foreach(ChannelEndpointElement endpoint in clientSettings.Endpoints)
                {
                    if (endpoint.Name == "WarrantyXServiceSoap")
                    {
                        address = endpoint.Address.ToString();   
                        break;
                    }
                }

                if (!string.IsNullOrEmpty(address))
                {
                    HttpWebRequest request;
                    string url = WebService URL;
                    string action = "http://tempuri.org/MethodName";

                    request = (HttpWebRequest)WebRequest.Create(url);
                    request.Method = "POST"; // OR "GET" if you want to make a GET request 
                    request.ContentType = "text/xml; charset=utf-8";
                    request.Headers.Add("SOAPAction: " + action);

                    StringBuilder soapRequest = new StringBuilder("");
                    soapRequest.Append("http://www.w3.org/2001/XMLSchema-instance\
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
                    soapRequest.Append("");
                    soapRequest.Append("http://tempuri.org/\
">");
// these are the parameters of web method to pass data from client machine
                    soapRequest.Append("" + _Name + "
");
                    soapRequest.Append("" + Convert.ToBase64String(_BinaryDoc) + "
");
                    soapRequest.Append("" + _ContentType + "
");
                    soapRequest.Append("" + _ID.ToString() + "
");

                    soapRequest.Append("
");
                    soapRequest.Append("
");
                    soapRequest.Append("
");

                    request.ContentLength = soapRequest.ToString().Length;
                    request.KeepAlive = false;
                    request.Timeout = System.Threading.Timeout.Infinite;
                    request.AllowWriteStreamBuffering = false;
                    request.ProtocolVersion = HttpVersion.Version10;
                    request.ServicePoint.ConnectionLeaseTimeout = 600000;
                    request.ServicePoint.MaxIdleTime = 600000;
                    request.Proxy = null;
                    request.ReadWriteTimeout = 600000;
                    request.Accept = "text/xml";

                    using (Stream requestStream = request.GetRequestStream())
                    {
                        using (StreamWriter requestStreamWriter = new StreamWriter(requestStream))
                        {
                            requestStreamWriter.Write(soapRequest.ToString());
                        }
                    }

                    string result = string.Empty;
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        StreamReader reader = new StreamReader(response.GetResponseStream());
                        result = reader.ReadToEnd();
                    }

                    var xDoc = XDocument.Parse(result);
                    _createdAnnotation = new Guid(xDoc.Root.Value);

                }

No comments:

Split the String values with a special character in MS Flow to convert this into Array

 Many times we have a requirement to prepare the Mailing address for some of the documents, suppose there are Address Line1, Address Line2, ...