c# - 使用 HTTP/1.0 的 Web 服务 HTTP 请求

标签 c# web-services http

我正在使用 C# 向合作伙伴网络服务发送 SOAP 请求,

public class EODReproting : System.Web.Services.WebService
{
    /// <summary>
    /// Execute a Soap WebService call
    /// </summary>
    [WebMethod(MessageName = "SendZRARequest")]
    public void Execute()
    {
            HttpWebRequest request = CreateWebRequest();
            XmlDocument soapEnvelopeXml = new XmlDocument();
            soapEnvelopeXml.LoadXml(@"
                <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"">
                <soapenv:Header/>
                <soapenv:Body>
                <ser:processPaymentNotificationReport>
                <!--Zero or more repetitions:-->
                <ser:pmtNotifyReport>
                <!--Optional:-->
                <xsd:amountPaid>XXXX</xsd:amountPaid>
                <!--Optional:-->
                <xsd:bankBranchCode>XXXXX</xsd:bankBranchCode>
                <!--Optional:-->
                <xsd:bankTransNo>XXXXX</xsd:bankTransNo>
                <!--Optional:-->
                <xsd:datePaid>XXXXXX</xsd:datePaid>
                <!--Optional:-->
                <xsd:paymentRegTransNo>XXXXXX</xsd:paymentRegTransNo>
                <!--Optional:-->
                <xsd:status>S</xsd:status>
                <!--Optional:-->
                <xsd:taxPayerName>BYAN MARTIN</xsd:taxPayerName>
                <!--Optional:-->
                <xsd:tin>1002760252</xsd:tin>
                <!--Optional:-->
            <xsd:transactionId>XXXXXXX</xsd:transactionId>
            </ser:pmtNotifyReport>
        </ser:processPaymentNotificationReport>
    </soapenv:Body>
</soapenv:Envelope>");


            using (Stream stream = request.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }

            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                {
                    string soapResult = rd.ReadToEnd();
                }
            }

        }

    }                    

    /// <summary>
    /// Create a soap webrequest to [Url]
    /// </summary>
    /// <returns></returns>
    public HttpWebRequest CreateWebRequest()
    {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://XX.XX.XX.XX:9999/ZraWebService/services/EODPaymentNotificationReportService.EODPaymentNotificationReportServiceHttpSoap11Endpoint/");
            webRequest.Headers.Add("SOAPAction", "urn:processPaymentNotificationReport");
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            webRequest.Proxy = null;

        return webRequest;
    }
  }
}

调用此方法时,我的日志文件夹中记录了一个错误,

2015.01.09 10:20:25.8485->The remote server returned an error: (500) Internal Server Error.   at      System.Net.HttpWebRequest.GetResponse()
at EODReporting.ZRAEODReproting.Execute()

合作伙伴告诉我使用 HTTP/1.0 发送请求,我想在上面的代码中这样做。我该怎么做?

最佳答案

只需在 HttpWebRequest 声明之后添加以下选项:

request.ProtocolVersion=HttpVersion.Version10;

关于c# - 使用 HTTP/1.0 的 Web 服务 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27856715/

相关文章:

java - 隐藏空 webservice 标签(jaxb 生成)

c# - HTTP 摘要认证 - 在多个 http 请求之间共享 MD5 实例

http - Mule HTTP POST 请求返回 302 页面重定向

c# - 有没有办法在 C# 中完成类似 List<object> list = new List<int>() 的事情?

c# - 如何使用 .Net 读取 .cds 数据库 (TClientDataSet)

c# - 如何更改 WPF 中绑定(bind)选项的默认值?

Java JSOUP如何在 header 中发送Origin

c# - 可以在 DebuggerDisplay 显示的文本中使用字符串格式吗?

c# - 使用 Web 服务 (asmx) 接受 json 请求

c# - 通过 HTTPS 使用 Web 服务时抛出 System.Net.WebException