使用存储过程从SQL Server 2008调用休息Web服务

时间:2014-08-26 17:40:46

标签: web-services sql-server-2008 rest stored-procedures

我有一个通过存储过程从SQL查询创建的xml。我需要使用该xml调用REST Web服务并获得响应。我正在使用SQL Server 2008.有没有办法从存储过程中执行此操作?

我一直看到使用SQL CLR访问Web服务的链接。在这些链接中,我需要创建一个SQL Server项目/ .NET框架。我在Visual Studio 2008或SQL Server Management Studio中看不到这些模板/选项。

有没有办法直接从存储过程调用此https休息Web服务?

===============

我正在使用SQL CLR来执行此操作。但不知何故,xml没有正确通过。我收到500错误。你能不能看看这段代码是否有什么问题。

public static void SampleWSPost(SqlString weburl, out SqlString returnval)
    {
        string url = Convert.ToString(weburl);
        string feedData = string.Empty;
        ASCIIEncoding encoding = new ASCIIEncoding();
        try
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            Stream stream = null;
            StreamReader streamReader = null;


            XmlTextReader reader = new XmlTextReader("D:\\CXmls\\crmXml.xml");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(reader);
            reader.Close();

            request = (HttpWebRequest)WebRequest.Create(url);
            CredentialCache mycache = new CredentialCache();
            mycache.Add(new Uri(url), "Basic", new NetworkCredential("username", "password"));   
            request.Credentials = mycache;
            request.Method = "POST";
            request.ContentType = "text/xml";

            string data = xmlDoc.InnerXml;
            byte[] bytedata = Encoding.UTF8.GetBytes(data);
            Stream newStream = request.GetRequestStream();
            newStream.Write(bytedata, 0, bytedata.Length);
            newStream.Close();

            response = (HttpWebResponse)request.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;
    }

0 个答案:

没有答案