title: Calling a web service method using httpwebrequest - Unable to connect to the remote server error
body: <p>I'm calling a specific method of my web service, but as soon as I create a new StreamWriter object, I get the above error. I'm not sure why this happens, as I've never done this before. The code I'm using to do this was written using this link - <a href="https://stackoverflow.com/questions/1609294/invoking-an-asp-net-web-service-method-via-an-http-request">Invoking an ASP.NET web service method via an http request</a> as reference. Here's the snippet -</p>
  
  <pre><code>var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost/Helpers/Symbology.asmx");
  httpWebRequest.Headers.Add("SOAPAction", "\"http://tempuri.org/GetRelatedSymbols\"");
  httpWebRequest.ContentType = "text/json";
  httpWebRequest.Method = "POST";
  JavaScriptSerializer serializer = new JavaScriptSerializer();
  
  *using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))*//error occurs here
  {
      string json = serializer.Serialize(relatedSymbols);
      streamWriter.Write(json);
  }
  var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  string responseText = String.Empty;
  using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
  {
       responseText = streamReader.ReadToEnd();
  }
  </code></pre>
  
  <h3>Heres a snippet of my web-service code -</h3>
  
  <pre><code>[WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  //[System.ComponentModel.ToolboxItem(false)]
  // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
  // [System.Web.Script.Services.ScriptService]
  public class Symbology: System.Web.Services.WebService
  {
  
      [WebMethod]
      public List&lt;string&gt; GetRelatedSymbols()
      {
          try
          {
               //code here
          }
          catch(){}
      }
  }
  </code></pre>
  
  <p>Can anyone help in pointing out what I may be doing wrong?</p>