title: Call web service from console application c#
body: <p>I want to call a salesforce web service, but I get this error: An unhandled exception of type 'System.Net.WebException' occurred in System.dll and Additional information: Error en el servidor remoto: (500) Error interno del servidor.</p>
  
  <p>but when I call the same web service in Java I don't get any error.</p>
  
  <p>This is the C# code I am using:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Net;
  using System.IO;
  
  namespace ConsoleApplication1
  {
      class Program
      {
  
          static String ST = "some_string";
          static String pwd = "password";
          static String userName = "myusername";
          static String SERVER_URL;
          static String SESSION_ID;
  
          static void Main(string[] args)
          {
              string url = " https://test.salesforce.com/services/Soap/u/30.0";
              string details = CallRestMethod(url);
          }
  
          public static string CallRestMethod(string url)
          {
              HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
              webrequest.Method = "POST";
              webrequest.ContentType = "text/xml;charset=UTF-8";
              webrequest.Headers.Add("SOAPAction", "\"\"");
  
              String input = "&lt;Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\"&gt;&lt;Header/&gt;&lt;Body&gt;&lt;login xmlns=\"urn:partner.soap.sforce.com\"&gt;&lt;username&gt;" + userName + "&lt;/username&gt;&lt;password&gt;" + pwd + ST + "&lt;/password&gt;&lt;/login&gt;&lt;/Body&gt;&lt;/Envelope&gt;";
              ASCIIEncoding encoding = new ASCIIEncoding();
              byte[] byte1 = encoding.GetBytes(input);
  
              webrequest.GetRequestStream().Write(byte1, 0, byte1.Length);
  
              /*Stream newStream = webrequest.GetRequestStream();
              newStream.Write(byte1, 0, byte1.Length);
              newStream.Close();*/
  
              Console.WriteLine(webrequest.Headers);
  
              HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
              Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
              StreamReader responseStream = new StreamReader(webresponse.GetResponseStream(), enc);
              string result = string.Empty;
              result = responseStream.ReadToEnd();
              webresponse.Close();
              return result;
          }
      }
  }
  </code></pre>