static string serviceKey = "본인이 받은 서비스키";
public static string CallWebService()
{
var _url = "http://openapi.kfda.go.kr:80/openapi/soap/drug/MdcinPrdlstInfoService?wsdl";
var _action = "http://service.MdcinPrdlstInfo.drug.kfda/getMdcinPrdlstList"; // namespace(xmlns:ser) / methodname
XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
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);
}
return 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";
return webRequest;
}
private static XmlDocument CreateSoapEnvelope()
{
XmlDocument soapEnvelop = new XmlDocument();
soapEnvelop.LoadXml(string.Format(
@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:head=""http://apache.org/headers"" xmlns:ser=""http://service.MdcinPrdlstInfo.drug.kfda/"">
<soapenv:Header>
<head:ComMsgHeader>
<RequestMsgID></RequestMsgID>
<ServiceKey>{0}</ServiceKey>
<!--Optional:-->
<RequestTime></RequestTime>
<!--Optional:-->
<CallBackURI></CallBackURI>
</head:ComMsgHeader>
</soapenv:Header>
<soapenv:Body>
<ser:getMdcinPrdlstList>
<!--Optional:-->
<arg0>
<entrps>바이엘</entrps>
<prduct>아스피린</prduct>
<now_page_no>1</now_page_no>
<page_mg>100</page_mg>
</arg0>
</ser:getMdcinPrdlstList>
</soapenv:Body>
</soapenv:Envelope>"
, serviceKey));
return soapEnvelop;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}