출처(C# soap) : http://stackoverflow.com/questions/4791794/client-to-send-soap-request-and-received-response


1. 사용하고 싶은 Open API 를 data.go.kr 에서 활용신청 한 후 인증키를 발급받는다.

2. 1에서 받는 인증키를 출처2의 사이트로 가서 URL, Decode, UTF-8 옵션을 선택한 후 문자열을 변경한다. 

3. SoapUI 프로그램을 설치 한 후 [New Soap Project] 사용할 이름과 WSDL URL을 적으면 해당하는 웹서비스의 함수명과 호출 XML 화면인 나오는데, 여기서 값을 넣어 재생버튼을 누르면 결과값을 받아서 눈으로 볼 수 있다.


아래는 C#으로 구현한 Open API 호출 소스다.

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);

    }

}



















Posted by motolies
,