2017-02-23 1 views
0

Ich versuche, eine Anfrage in einer C# -Konsolenanwendung zu posten, und ich erhalte diesen Fehler. Die Anfrage arbeitet in SOAPUI, es gibt keine WSDL-Datei, daher muss ich das XML direkt in den Code laden (ich vermute sehr, dass dies der Ursprung des Fehlers ist).Fehler beim Senden der SOAP-Anforderung in der C# -Konsolenanwendung

Die Ausgabe auf der Konsole-Anwendung unter: enter image description here

Hier ist die C# Konsole-Anwendung, die ich verwende diese SOAP-Anforderung zu senden: Error Message On Console

Der SOAPUI Antrag, der wie folgt funktioniert ist.

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Text; 
using System.Threading.Tasks; 
using System.Xml; 

namespace IMSPostpaidtoPrepaid 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Program obj = new Program(); 
      obj.createSOAPWebRequest(); 
     } 

     //Create the method that returns the HttpWebRequest 
     public void createSOAPWebRequest() 
     { 

      //Making the Web Request 
      HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(@"http://127.1.1.0:9090/spg"); 

      //Content Type 
      Req.ContentType = "text/xml;charset=utf-8"; 
      Req.Accept = "text/xml"; 

      //HTTP Method 
      Req.Method = "POST"; 

      //SOAP Action 
      Req.Headers.Add("SOAPAction", "\"SOAPAction:urn#LstSbr\""); 

      NetworkCredential creds = new NetworkCredential("username", "pass"); 
      Req.Credentials = creds; 

      Req.Headers.Add("MessageID", "1"); //Adding the MessageID in the header 

      string DN = "+26342720450"; 
      string DOMAIN = "ims.telone.co.zw"; 

      //Build the XML 
      string xmlRequest = String.Concat(
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>", 
       "<soap:Envelope", 
       "  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", 
       "  xmlns:m=\"http://www.huawei.com/SPG\"", 
       "  xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", 
       "  xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">", 
       " <soap:Body>", 
       "<m:LstSbr xmlns=\"urn#LstSbr\">", 
       " <m:DN>", DN, "</ m:DN>", 
       " <m:DOMAIN>", DOMAIN, "</ m:DOMAIN>", 
       " </m:LstSbr>", 
       " </soap:Body>", 
       "</soap:Envelope>"); 


      //Pull Request into UTF-8 Byte Array 
      byte[] reqBytes = new UTF8Encoding().GetBytes(xmlRequest); 

      //Set the content length 
      Req.ContentLength = reqBytes.Length; 

      //Write the XML to Request Stream 
      try 
      { 
       using (Stream reqStream = Req.GetRequestStream()) 
       { 
        reqStream.Write(reqBytes, 0, reqBytes.Length); 
       } 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine("Exception of type " + ex.GetType().Name + " " + ex.Message); 
       Console.ReadLine(); 
       throw; 
      } 

      //Headers and Content are set, lets call the service 
      HttpWebResponse resp = (HttpWebResponse)Req.GetResponse(); 

      string xmlResponse = null; 

      using (StreamReader sr = new StreamReader(resp.GetResponseStream())) 
      { 
       xmlResponse = sr.ReadToEnd(); 
      } 
      Console.WriteLine(xmlResponse); 
      Console.ReadLine(); 

     } 

    } 
} 

Antwort

0

Hier ist meine grundlegende Methode, die ich verwenden, um einen Webdienst zu veröffentlichen.

static XNamespace soapNs = "http://schemas.xmlsoap.org/soap/envelope/"; 
static XNamespace topNs = "Company.WebService"; 

private System.Net.HttpWebResponse ExecutePost(string webserviceUrl, string soapAction, string postData) { 
    var webReq = (HttpWebRequest)WebRequest.Create(webserviceUrl); 
    webReq.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; 
    webReq.ContentType = "text/xml;charset=UTF-8"; 
    webReq.UserAgent = "Opera/12.02 (Android 4.1; Linux; Opera Mobi/ADR-1111101157; U; en-US) Presto/2.9.201 Version/12.02"; 
    webReq.Referer = "http://www.company.com"; 
    webReq.Headers.Add("SOAPAction", soapAction); 
    webReq.Method = "POST"; 

    var encoded = Encoding.UTF8.GetBytes(postData); 
    webReq.ContentLength = encoded.Length; 
    var dataStream = webReq.GetRequestStream(); 
    dataStream.Write(encoded, 0, encoded.Length); 
    dataStream.Close(); 

    System.Net.WebResponse response; 
    try { response = webReq.GetResponse(); } 
    catch { 
     ("Unable to post:\n" + postData).Dump(); 
     throw; 
    } 

    return response as System.Net.HttpWebResponse; 
} 

Ich kann dann bauen, was ich will, um dies zu tun, was ich brauche. Zum Beispiel hier ist, wie ich es benutzen würde.

private void CreateTransaction(string webServiceUrl, string ticket, double costPerUnit) { 
    string soapAction = "Company.WebService/ICompanyWebService/CreatePurchaseTransaction"; 
    var soapEnvelope = new XElement(soapNs + "Envelope", 
         new XAttribute(XNamespace.Xmlns + "soapenv", "http://schemas.xmlsoap.org/soap/envelope/"), 
         new XAttribute(XNamespace.Xmlns + "top", "Company.WebService"),      
        new XElement(soapNs + "Body", 
         new XElement(topNs + "CreatePurchaseTransaction", 
          new XElement(topNs + "command", 
           new XElement(topNs + "BillTransaction", "true"), 
           new XElement(topNs + "BillingComments", "Created By C# Code"), 
           new XElement(topNs + "Department", "Development"), 
           new XElement(topNs + "LineItems", GetManualPurchaseLineItems(topNs, costPerUnit)),         
           new XElement(topNs + "Surcharge", "42.21"), 
           new XElement(topNs + "Tax", "true"), 
           new XElement(topNs + "TransactionDate", DateTime.Now.ToString("yyyy-MM-dd")) 
        )))); 

    var webResp = ExecutePost(webServiceUrl, soapAction, soapEnvelope.ToString()); 
    if (webResp.StatusCode != HttpStatusCode.OK) 
     throw new Exception("Unable To Create The Transaction"); 

    var sr = new StreamReader(webResp.GetResponseStream()); 
    var xDoc = XDocument.Parse(sr.ReadToEnd()); 
    var result = xDoc.Descendants(topNs + "ResultType").Single(); 
    if (result.Value.Equals("Success", StringComparison.CurrentCultureIgnoreCase) == false) 
     throw new Exception("Unable to post the purchase transaction. Look in the xDoc for more details."); 
} 

private IEnumerable<XElement> GetManualPurchaseLineItems(XNamespace topNs, double costPerUnit) { 
    var lineItems = new List<XElement>(); 

    lineItems.Add(new XElement(topNs + "PurchaseLineItem", 
         new XElement(topNs + "Count", "5"), 
         new XElement(topNs + "ExpenseClass", "Tech Time"), 
         new XElement(topNs + "ItemName", "Brushing and Flossing"), 
         new XElement(topNs + "Location", "Your House"), 
         new XElement(topNs + "UnitCost", costPerUnit.ToString("#.00")))); 

    return lineItems; 
} 

Sie müssten dies in Ihre Konsolenanwendung anpassen, aber hilft Ihnen das dabei?

+0

Wirf einen Blick darauf – nktsamba

+0

Hey @nktsamba, hast du eine Chance bekommen, es auszuarbeiten? –

Verwandte Themen