<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>How to POST to a generic Web Service</Title>
      <Shortcut>HowtoPOSTtoagenericWebService</Shortcut>
      <Description>How to POST to a generic Web Service [C#]</Description>
      <Author>Fabian Nicollier</Author>
      <HelpUrl>/PreviewSnippet.aspx?SnippetID=6e560977-5158-424d-b565-0af5991d5492</HelpUrl>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="csharp"><![CDATA[ASCIIEncoding encoding = new ASCIIEncoding();
HttpWebResponse httpResponse = null;
//Parameter string should look like "parameter1=value1&parameter2=value2"
byte[] btParameters = encoding.GetBytes(sParameterString);
httpRequest.Method = "POST";
httpRequest.KeepAlive = false;
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = btParameters.Length;
httpRequest.Timeout = 30000;
//Create the request
using (HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(this.URL + "/" + this.WebServiceMethodName))
            {                
                //Add parameters         
                using (Stream requestStream = httpRequest.GetRequestStream())
                {
                    requestStream.Write(btParameters, 0, btParameters.Length);                    
                }
                //Send the request                
                try
                {
                    httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                    //Process the response
                    string response;
                    using (Stream baseStream = httpResponse.GetResponseStream())
                    {
                        using (System.IO.StreamReader responseStreamReader = new System.IO.StreamReader(baseStream))
                        {
                            //The response
                            response = responseStreamReader.ReadToEnd();
                        }
                    }
                }
                catch (Exception ex)
                {                
                    //Error
                }
                finally
                {
                    if (httpResponse != null)
                        httpResponse.Close();
                }
            }]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>