如何形成请求URL以从WSDL获取响应c#

时间:2013-08-29 07:29:29

标签: c# xml web-services soap wsdl

My Xml Somthing Like this..


<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"            xmlns:b2b="http://www.apollomunichinsurance.com/B2BService"   xmlns:prop="http://schemas.datacontract.org/2004/07/ProposalCaptureServiceLibrary"   xmlns:ser="http://schemas.datacontract.org/2004/07/ServiceObjects">
 <soapenv:Header />
<soapenv:Body>
<b2b:ProposalCapture>
   <b2b:ProposalCaptureServiceRequest>
    <prop:Action>Create</prop:Action>
    <prop:Partner>
      <ser:PartnerCode>1000</ser:PartnerCode>
      <ser:Password>test123</ser:Password>
      <ser:UserName>xxxx</ser:UserName>
     </prop:Partner>

    </b2b:ProposalCaptureServiceRequest>
  </b2b:ProposalCapture>
</soapenv:Body>

And  My C# Code IS

public XmlDocument PostXMLTransaction_HDFC(string v_strURL)
{

    XmlDocument XMLResponse = null;

    //Declare an HTTP-specific implementation of the WebRequest class.
    HttpWebRequest objHttpWebRequest;

    //Declare an HTTP-specific implementation of the WebResponse class
    HttpWebResponse objHttpWebResponse = null;

    //Declare a generic view of a sequence of bytes
    Stream objRequestStream = null;
    Stream objResponseStream = null;

    //Declare XMLReader
    XmlTextReader objXMLReader;

    //Creates an HttpWebRequest for the specified URL.
    objHttpWebRequest = (HttpWebRequest)WebRequest.Create(v_strURL);

    try
    {
        //---------- Start HttpRequest 

        //Set HttpWebRequest properties
        byte[] bytes;
        XmlDocument v_objXMLDoc = new XmlDocument();
        v_objXMLDoc.Load("http://localhost:58110/OnlineInsurance/XMLFile3.xml");
        bytes = System.Text.Encoding.ASCII.GetBytes(v_objXMLDoc.InnerXml);
        objHttpWebRequest.Method = "POST";
        objHttpWebRequest.ContentLength = bytes.Length;
        objHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";

        //Get Stream object 
        objRequestStream = objHttpWebRequest.GetRequestStream();

        //Writes a sequence of bytes to the current stream 
        objRequestStream.Write(bytes, 0, bytes.Length);

        //Close stream
        objRequestStream.Close();

        //---------- End HttpRequest

        //Sends the HttpWebRequest, and waits for a response.
        objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();

        //---------- Start HttpResponse
        if (objHttpWebResponse.StatusCode == HttpStatusCode.OK)
        {
            //Get response stream 
            objResponseStream = objHttpWebResponse.GetResponseStream();

            //Load response stream into XMLReader
            objXMLReader = new XmlTextReader(objResponseStream);

            //Declare XMLDocument
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(objXMLReader);

            //Set XMLResponse object returned from XMLReader
            XMLResponse = xmldoc;

            //Close XMLReader
            objXMLReader.Close();
        }

        //Close HttpWebResponse
        objHttpWebResponse.Close();
    }
    catch (WebException e)
    {
        //TODO: Add custom exception handling
        if (e.Status == WebExceptionStatus.ProtocolError)
        {
            WebResponse resp = e.Response;
            using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
            {
                string s = sr.ReadToEnd();
            }
        }
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
    finally
    {
        //Close connections
        objRequestStream.Close();
        objResponseStream.Close();
        objHttpWebResponse.Close();

        //Release objects
        objXMLReader = null;
        objRequestStream = null;
        objResponseStream = null;
        objHttpWebResponse = null;
        objHttpWebRequest = null;
    }

    //Return
    return XMLResponse;
} 

和Am调用此PostXMLTransaction_HDFC(“http://xxx.xxx.com/ProposalCaptureService/ProposalCapture”);

ProposalCaptureService -WSDL名称

ProposalCapture-操作名称

它将返回远程服务器返回错误:(404)Not Found。

0 个答案:

没有答案