从使用SQL CLR的存储过程调用REST Web服务时出现500错误

时间:2014-09-03 14:32:54

标签: c# rest stored-procedures sql-server-2008-r2

我在存储过程(SQL Server 2008)中使用SQL-CLR过程(创建了一个dll并将其用作程序集)来将xml传递给其他Web服务。我总是得到一个错误500 - 我传递字符串的方式有问题吗?

我确信它已经过身份验证,因为如果我输入错误的用户标识和密码,我会收到错误。有人可以帮忙吗?

这是SQL-CLR过程,下面是我从存储过程调用的方法:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;

public partial class StoredProcCM
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void SampleWSPost(SqlString weburl, String xmlIn, out SqlString returnval)
    {
        string url = Convert.ToString(weburl);
        string feedData = string.Empty;
        ASCIIEncoding encoding = new ASCIIEncoding();

        try
        {
            HttpWebResponse response = null;
            Stream stream = null;
            StreamReader streamReader = null;

            byte[] data = encoding.GetBytes(xmlIn);
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
            CredentialCache mycache = new CredentialCache();
            mycache.Add(new Uri(url), "Basic", new NetworkCredential("username", "password"));   
            webrequest.Credentials = mycache;

            // set method as post
            webrequest.Method = "POST";
            // set content type
            webrequest.ContentType = "application/x-www-form-urlencoded";
            // set content length
            webrequest.ContentLength = data.Length;
            // get stream data out of webrequest object
            Stream newStream = webrequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            // declare & read response from service

            //------------------------
            response = (HttpWebResponse)webrequest.GetResponse();
            stream = response.GetResponseStream();
            streamReader = new StreamReader(stream);
            feedData = streamReader.ReadToEnd();
            response.Close();
            stream.Dispose();
            streamReader.Dispose();
        }
        catch (Exception ex)
        {
            SqlContext.Pipe.Send(ex.Message.ToString());
        }

        returnval = feedData;
    }
}; 

我使用csc创建一个dll,并在存储过程中使用此dll,如下所示:@XmlString具有xml

SET @webUrl = 'https://test.abc.com:501/XISOAPAdapter/MessageServlet?channel=:CHANNEL1:TEST_SOAP_Sender'

exec @res= SampleWSPost @webUrl,@XmlString,@res

0 个答案:

没有答案