1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| // Trust all certificates. This is just for sample code.
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
IWebProxy proxy_sbe = webRequest.Proxy;
if (proxy_sbe != null)
{
string proxyuri = proxy_sbe.GetProxy(webRequest.RequestUri).ToString();
webRequest.UseDefaultCredentials = true;
webRequest.Proxy = new WebProxy(proxyuri, false);
webRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
}
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
// suspend this thread until call is complete. You might want to
// do something usefull here like update your UI.
asyncResult.AsyncWaitHandle.WaitOne();
// get the response from the completed web request.
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
Console.Write(soapResult);
}
}
private static HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", action);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
webRequest.Timeout = 100000;
webRequest.MaximumResponseHeadersLength = 3000;
return webRequest;
}
private static XmlDocument CreateSoapEnvelope()
{
XmlDocument soapEnvelop = new XmlDocument();
String userName = "";
String password = "";
String corporationCode = "";
String serviceTypeCode = "GS";
String langue = "fr";
String requestId = "ALL";
String stockCode = "ACE";
string soap_env = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ace=""http://adresse site"">
<soapenv:Header/>
<soapenv:Body>
<ace:getStockInfo>
<header>
<userName>" + userName + @"</userName>
<password>" + password + @"</password>
<corporationCode>" + corporationCode + @"</corporationCode>
<serviceTypeCode>" + serviceTypeCode + @"</serviceTypeCode>
<langue>" + langue + @"</langue>
<requestId>" + requestId + @"</requestId>
<requestDate>" + DateTime.Now.ToString("o") + "</requestDate>" +
@"</header>
<stockCode>" + stockCode + @"</stockCode>";
soap_env += @"<manufacturerCode>AP</manufacturerCode>";
//if (modelCode != String.Empty)
// soap_env += "<modelCode>" + modelCode + "</modelCode>";
soap_env += @"</ace:getStockInfo>
</soapenv:Body>
</soapenv:Envelope>";
soapEnvelop.LoadXml(soap_env);
return soapEnvelop;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
} |
Partager