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

时间:2010-03-17 06:12:53

标签: c# asmx

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”任何人都可以帮助我吗???

1 个答案:

答案 0 :(得分:0)

内部服务器错误只是意味着服务器上出现了问题。它通常意味着服务器抛出了一个未处理的异常。

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

顺便说一下,你的代码非常糟糕。

  • 所有IDisposable类都应在using块中实例化。
  • “throw ex”只会弄乱你的筹码。完全摆脱那种尝试/捕获。