SOAP Request for Quest Diagnostics出现问题(使用Visual Studio 2010、

本文关键字:使用 Visual Studio 2010 问题 for Request Quest Diagnostics SOAP | 更新日期: 2023-09-27 18:21:12

我是SOAP的新手,正在尝试向Quest Diagnostics发出请求,但似乎不起作用。我在尝试发出请求时遇到500内部服务器错误。

这是我的代码:

/// <summary>
/// Test SOAP Request
/// </summary>
/// <returns>A boolean value indicating whether the function executed successfully or not.</returns>
private bool TestSoap()
{
    // declare necessary objects and variables
    MailMessage mailMsg;
    HttpWebRequest webRequest;
    SmtpClient smtpClient = new SmtpClient();
    ESServiceSoapClient soapClient = new ESServiceSoapClient();
    XmlDocument soapEnvelope = new XmlDocument();
    string soapResult;
    webRequest = (HttpWebRequest)WebRequest.Create("https://qcs-uat.questdiagnostics.com/services/esservice.asmx");
    webRequest.Headers.Add("SOAPAction", "https://qcs-uat.questdiagnostics.com/services/esservice.asmx?WSDL");
    webRequest.ContentType = "text/xml;charset='"utf-8'"";
    webRequest.Accept = "text/xml";
    webRequest.Method = "POST";
    soapEnvelope.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?><soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:wss=""http://wssim.labone.com/""><soapenv:Header/><soapenv:Body><wss:FullRetrieveCollectionSiteDetails><wss:username>#user name#</wss:username><wss:password># password #</wss:password></wss:FullRetrieveCollectionSiteDetails></soapenv:Body></soapenv:Envelope>");
    using (Stream stream = webRequest.GetRequestStream())
    {
        soapEnvelope.Save(stream);
    }
    // attempt async call
    IAsyncResult asyncReslt = webRequest.BeginGetResponse(null, null);
    // suspend
    asyncReslt.AsyncWaitHandle.WaitOne();
    // get soap result
    using (WebResponse webResponse = webRequest.EndGetResponse(asyncReslt))
    {
        using (StreamReader sReader = new StreamReader(webResponse.GetResponseStream()))
        {
            soapResult = sReader.ReadToEnd();
        }
    }
    mailMsg = new MailMessage("info@evance.com", "rchristiansen@evance.com");
    mailMsg.Subject = "SOAP XML Test Request - Quest Diagnostics";
    mailMsg.Body = soapResult;
    // attempt to send message
    try
    {
        smtpClient.Send(mailMsg);
    }
    catch (Exception ex)
    {
        this.lblError.Text = "Unable to send report email to applicant: " + ex.Message;
        return false;
    }
    // return true - function executed successfully
    return true;
}

我删除了用户名和密码,这样你就不会试图自己访问它了。再说一遍,我是SOAP的新手,我不知道到底出了什么问题。请注意,我确实有一些未使用的代码,例如ESServiceSoapClient。对象中有一个为函数指定的方法,但也不起作用(为此得到了EndpointNotFoundException)。

SOAP Request for Quest Diagnostics出现问题(使用Visual Studio 2010、

尝试更改此代码

// get soap result
using (WebResponse webResponse = webRequest.EndGetResponse(asyncReslt))
{
    using (StreamReader sReader = new StreamReader(webResponse.GetResponseStream()))
    {
        soapResult = sReader.ReadToEnd();
    }
}

对此:

// get soap result
using (WebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncReslt))
{
    using (Stream dataStream = webResponse.GetResponseStream())
    {
        StreamReader sReader = new StreamReader(webResponse.GetResponseStream())
        soapResult = sReader.ReadToEnd();
    }
}

更改:直接将webRequest转换为webResponse,为getResponseStream方法添加了一行

loadXml开头的@符号也应该在那里吗?

参考文献:

.NET线程-HttpWebRequest BeginGetResponse+AutoResetEvent

使用HttpWebRequest类