Reputation: 41
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.
but when I call the same web service in Java I don't get any error.
This is the C# code I am using:
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 = "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\"><Header/><Body><login xmlns=\"urn:partner.soap.sforce.com\"><username>" + userName + "</username><password>" + pwd + ST + "</password></login></Body></Envelope>";
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;
}
}
}
Upvotes: 0
Views: 14964
Reputation: 41
Finally I can connect to the web service I added this line:
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
this line is used to use the protocol TLS 1.2
Upvotes: 4
Reputation: 1459
You would be better off using a WSDL and creating a Service Reference rather than building out the request by hand.
More details: https://msdn.microsoft.com/en-us/library/cc636424(v=ax.50).aspx
Upvotes: 0