Как отправлять / получать запросы и ответы SOAP с помощью C #?

private static string WebServiceCall(string methodName)        
{
    WebRequest webRequest = WebRequest.Create("http://localhost/AccountSvc/DataInquiry.asmx");
    HttpWebRequest httpRequest = (HttpWebRequest)webRequest;             
    httpRequest.Method = "POST";             
    httpRequest.ContentType = "text/xml; charset=utf-8";
    httpRequest.Headers.Add("SOAPAction: http://tempuri.org/" + methodName);
    httpRequest.ProtocolVersion = HttpVersion.Version11;
    httpRequest.Credentials = CredentialCache.DefaultCredentials;
    Stream requestStream = httpRequest.GetRequestStream();              
    //Create Stream and Complete Request             
    StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);

    StringBuilder soapRequest = new StringBuilder("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
    soapRequest.Append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
    soapRequest.Append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>");
    soapRequest.Append("<GetMyName xmlns=\"http://tempuri.org/\"><name>Sam</name></GetMyName>");
    soapRequest.Append("</soap:Body></soap:Envelope>");

    streamWriter.Write(soapRequest.ToString());             
    streamWriter.Close();              
    //Get the Response    
    HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
    StreamReader srd = new StreamReader(wr.GetResponseStream()); 
    string resulXmlFromWebService = srd.ReadToEnd(); 
    return resulXmlFromWebService;
}

Я пробовал другой код для отправки / получения ответов мыла, но все терпят неудачу с одним и тем же «Удаленный сервер возвратил ошибку: (500) Внутренняя ошибка сервера». .

Я могу получить доступ к той же службе с помощью SoapUI. Я тоже могу вызвать метод. Я читал на этом форуме, что причиной ошибки 500 может быть неправильный заголовок. Заголовок проверил, вроде нормально. Буду признателен, если кто-нибудь может помочь.

Ниже приводится пример запроса SOAP:

POST /AccountSvc/DataInquiry.asmx HTTP/1.1
Host: abc.def.gh.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetMyName"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetMyName xmlns="http://tempuri.org/">
      <name>string</name>
    </GetMyName>
  </soap:Body>
</soap:Envelope>

Я использовал приведенный выше пример запроса для выполнения метода, и он сработал. Вот переданный мной запрос Soap:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><GetMyName xmlns="http://tempuri.org/"><name>Sam</name></GetMyName></soap:Body></soap:Envelope>

Изменить:

Я обновил приведенный выше код в WebServiceCall, который работал для службы .asmx. Но тот же код не работал для службы WCF. Почему?

22
задан John Saunders 5 August 2013 в 23:56
поделиться