动态请求输入的 Web 服务请求问题

发布于 2024-08-25 14:47:13 字数 2816 浏览 4 评论 0原文

try
{
    const string siteURL = "http://ops.epo.org/2.6.1/soap-services/document-retrieval";
    const string docRequest = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><document-retrieval id='EP        1000000A1 I ' page-number='1' document-format='SINGLE_PAGE_PDF' system='ops.epo.org' xmlns='http://ops.epo.org' /></soap:Body></soap:Envelope>";

    var request = (HttpWebRequest)WebRequest.Create(siteURL);
    request.Method = "POST";
    request.Headers.Add("SOAPAction", "\"document-retrieval\"");
    request.ContentType = " text/xml; charset=utf-8";


    Stream stm = request.GetRequestStream();
    byte[] binaryRequest = Encoding.UTF8.GetBytes(docRequest);
    stm.Write(binaryRequest, 0, docRequest.Length);
    stm.Flush();
    stm.Close();
    var memoryStream = new MemoryStream();
    WebResponse resp = request.GetResponse();
    var buffer = new byte[4096];
    Stream responseStream = resp.GetResponseStream();
    {
        int count;
        do
        {
            count = responseStream.Read(buffer, 0, buffer.Length);
            memoryStream.Write(buffer, 0, count);
        } while (count != 0);
    }
    resp.Close();
    byte[] memoryBuffer = memoryStream.ToArray();
    System.IO.File.WriteAllBytes(@"E:\sample12.pdf", memoryBuffer);

}
catch (Exception ex)
{
throw ex;
}

上面的代码是检索 pdf webresponse。只要请求保持不变,它就可以正常工作,

const string docRequest = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><document-retrieval id='EP        1000000A1 I ' page-number='1' document-format='SINGLE_PAGE_PDF' system='ops.epo.org' xmlns='http://ops.epo.org' /></soap:Body></soap:Envelope>";

但是如何通过动态请求检索相同的内容。当上面的代码更改为接受动态输入时,例如,

[WebMethod]
public string DocumentRetrivalPDF(string docid, string pageno, string docFormat, string fileName)
{
    try
    {
         ........
         .......
         string docRequest = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><document-retrieval id=" + docid + " page-number=" + pageno + " document-format=" + docFormat + " system='ops.epo.org' xmlns='http://ops.epo.org' /></soap:Body></soap:Envelope>";

         ......
         ........
     return "responseTxt";
    }
    catch (Exception ex)
    {
        return ex.Message;
    }

  }

它返回“内部服务器错误:500”任何人都可以帮助我吗???

try
{
    const string siteURL = "http://ops.epo.org/2.6.1/soap-services/document-retrieval";
    const string docRequest = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><document-retrieval id='EP        1000000A1 I ' page-number='1' document-format='SINGLE_PAGE_PDF' system='ops.epo.org' xmlns='http://ops.epo.org' /></soap:Body></soap:Envelope>";

    var request = (HttpWebRequest)WebRequest.Create(siteURL);
    request.Method = "POST";
    request.Headers.Add("SOAPAction", "\"document-retrieval\"");
    request.ContentType = " text/xml; charset=utf-8";


    Stream stm = request.GetRequestStream();
    byte[] binaryRequest = Encoding.UTF8.GetBytes(docRequest);
    stm.Write(binaryRequest, 0, docRequest.Length);
    stm.Flush();
    stm.Close();
    var memoryStream = new MemoryStream();
    WebResponse resp = request.GetResponse();
    var buffer = new byte[4096];
    Stream responseStream = resp.GetResponseStream();
    {
        int count;
        do
        {
            count = responseStream.Read(buffer, 0, buffer.Length);
            memoryStream.Write(buffer, 0, count);
        } while (count != 0);
    }
    resp.Close();
    byte[] memoryBuffer = memoryStream.ToArray();
    System.IO.File.WriteAllBytes(@"E:\sample12.pdf", memoryBuffer);

}
catch (Exception ex)
{
throw ex;
}

The code above is to retrieve the pdf webresponse.It works fine as long as the request remains canstant,

const string docRequest = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><document-retrieval id='EP        1000000A1 I ' page-number='1' document-format='SINGLE_PAGE_PDF' system='ops.epo.org' xmlns='http://ops.epo.org' /></soap:Body></soap:Envelope>";

but how to retrieve the same with dynamic requests. When the above code is changed to accept dynamic inputs like,

[WebMethod]
public string DocumentRetrivalPDF(string docid, string pageno, string docFormat, string fileName)
{
    try
    {
         ........
         .......
         string docRequest = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><document-retrieval id=" + docid + " page-number=" + pageno + " document-format=" + docFormat + " system='ops.epo.org' xmlns='http://ops.epo.org' /></soap:Body></soap:Envelope>";

         ......
         ........
     return "responseTxt";
    }
    catch (Exception ex)
    {
        return ex.Message;
    }

  }

It return an "INTERNAL SERVER ERROR:500" can anybody help me on this???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舟遥客 2024-09-01 14:47:13

内部服务器错误仅仅意味着服务器上出现问题。这通常意味着服务器抛出了未处理的异常。

在 Windows 事件日志中查找答案。特别是查看应用程序事件日志。

顺便说一句,你的代码很糟糕。

  • 所有 IDisposable 类都应在 using 块中实例化。
  • “扔前”只会弄乱你的堆栈。完全摆脱 try/catch 。

Internal Server Error simply means there's something wrong on the server. It usually means that the server has thrown an exception which was not handled.

Look in the Windows Event logs for an answer. In particular, look in the Application event log.

By the way, your code is pretty bad.

  • All the IDisposable classes should be instantiated in using blocks.
  • "throw ex" just messes up your stack. Get rid of that try/catch entirely.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文