Web服务SOAP请求/响应

时间:2015-01-06 11:24:38

标签: c# web-services soap

作为一名初学者,我之前已经开发过webservices并将其消费。但我还没有开发一个发送请求并从合作伙伴Web服务获得响应的方法。我在互联网上搜索过,我已经能够做到这一点。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Web.Configuration;
using System.Net;
using System.IO;
using System.Data.OracleClient;
using System.Text;

namespace ZRAEODReporting
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class ZRAEODReproting : System.Web.Services.WebService
{

    [WebMethod]
    public static void SendZRARequest()
    {
        string amountPaid = "";
        string bankBranchCode = "03673"; 
        string bankTransNo = "5667";
        string datepaid = "";
        string paymentRegTransNo = "11198854";
        string status = "S";
        string taxPayerName = "";
        string tin = "1002760252"; 
        string transactionId = "";
        string partition = "ACTION_MAP_" + DateTime.Now.Year + "_" + DateTime.Now.Month + DateTime.Now.Day;
        //string zramsisdn = WebConfigurationManager.AppSettings["DEST_MN"];
        //string zramsisdn = "789654327828";
        string xml = "";
        bool flag = false;

        //Fetch Records
        try
        {
            //String SQL = String.Format("SELECT ACTION_MN_ID,SOCE_MN,COST,REFERENCE_ID, CREATED_TIMESTAMP, STATUS_DESCRIPTION,INFORMATION,(SELECT FIRST_NAME|| ' ' ||SECOND_NAME||' '||LAST_NAME FROM INFO WHERE NUMBER = SOURCE_MN) AS NAME FROM ACTION_CH WHERE DEST_MN = '7846537263737' AND REFERENCE_ID = '123456781'");

            Util.LogMessage(SQL, "Received", "PP");
            using (OracleConnection conn = new OracleConnection(clsOracleTasks.GetDatabaseServerConfigs()))
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                OracleDataAdapter adapt = new OracleDataAdapter(SQL, conn);
                using (DataTable table = new DataTable())
                {
                    adapt.Fill(table);
                    //Loop In Result
                    foreach (DataRow row in table.Rows)
                    {
                        amountPaid = row["COUNT"].ToString();
                        taxPayerName = row["NAME"].ToString();
                        datepaid = row.Field<DateTime>("CREATED_TIMESTAMP").ToString("yyyyMMdd");
                        //paymentRegTransNo = row["INFORMATION"].ToString();
                        //status = row["STATUS_DESCRIPTION"].ToString();
                        transactionId = row["REFERENCE_ID"].ToString();
                        //tin = row["ACTION_CASH_ID"].ToString();

                        //Create XML
                        xml = "";
                        xml = xml + "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ser='http://service.bank.pmt.zra' xmlns:xsd='http://bean.bank.pmt.zra/xsd'>";
                        xml = xml + "<soapenv:Header/>";
                        xml = xml + "<soapenv:Body>";
                        xml = xml + "<ser:processPaymentNotificationReport>";
                        xml = xml + "<!--Zero or more repetitions:-->";
                        xml = xml + "<ser:pmtNotifyReport>";
                        xml = xml + "<!--Optional:-->";
                        xml = xml + "<xsd:amountPaid>" + amountPaid + "</xsd:amountPaid>";
                        xml = xml + "<!--Optional:-->";
                        xml = xml + "<xsd:bankBranchCode>" + bankBranchCode + "</xsd:bankBranchCode>";
                        xml = xml + "<!--Optional:-->";
                        xml = xml + "<xsd:bankTransNo>" + bankTransNo + "</xsd:bankTransNo>";
                        xml = xml + "<!--Optional:-->";
                        xml = xml + "<xsd:datePaid>" + datepaid + "</xsd:datePaid>";
                        xml = xml + "<!--Optional:-->";
                        xml = xml + "<xsd:paymentRegTransNo>" + paymentRegTransNo + "</xsd:paymentRegTransNo>";
                        xml = xml + "<!--Optional:-->";
                        xml = xml + "<xsd:status>" + status + "</xsd:status>";
                        xml = xml + "<!--Optional:-->";
                        xml = xml + "<xsd:taxPayerName>" + taxPayerName + "</xsd:taxPayerName>";
                        xml = xml + "<!--Optional:-->";
                        xml = xml + "<xsd:tin>" + tin + "</xsd:tin>";
                        xml = xml + "<!--Optional:-->";
                        xml = xml + "<xsd:transactionId>" + transactionId + "</xsd:transactionId>";
                        xml = xml + "</ser:pmtNotifyReport>";
                        xml = xml + "</ser:processPaymentNotificationReport>";
                        xml = xml + "</soapenv:Body>";
                        xml = xml + "</soapenv:Envelope>";

                        //Util.WriteApplicationLog(xml);
                        Util.LogMessage(xml, "ErrorLogs", "PP");
                        //Call SOAP Action
                        flag = sendSOAP(xml);
                    }
                }
            }

        }

        catch (Exception ex)
        {
            Util.LogMessage(ex.Message + ex.StackTrace, "ErrorLogs", "PP");
        }
    }

    [WebMethod]
    internal static bool sendSOAP(string xml)
    {
        bool flag = false;
        //string url = WebConfigurationManager.AppSettings["ZRA_URL"];
        try
        {
            flag = true;

            clsConfig.InitiateSSLTrust();
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://101.168.88.69:9876/qraWebService/services/EODPaymentNotificationReportService.EODPaymentNotificationReportServiceHttpSoap11Endpoint/");
            req.Headers.Add("SOAPAction", "urn:processPaymentNotificationReport");
            req.ContentType = "text/xml;charset=\"utf-8\"";
            req.Accept = "text/xml";
            req.Method = "POST";
            req.Proxy = null;

            try
            {
                using (Stream stm = req.GetRequestStream())
                {
                    using (StreamWriter stmw = new StreamWriter(stm))
                    {
                        stmw.Write(xml);
                    }
                }

                WebResponse response = req.GetResponse();

                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                string responseString = reader.ReadToEnd();
                Util.LogMessage(responseString, "Received", "PP");
            }
            catch (Exception e)
            {
                flag = false;
                Util.LogMessage(e.Message + e.StackTrace, "ErrorLogs", "PP");
            }
            return flag;
        }
        finally { }
    }
   }
 }

我正在尝试运行鳕鱼,但我的错误日志或合作伙伴的回复都没有收到任何内容。我的代码出了什么问题?

0 个答案:

没有答案