Sending HTTP POST request from SQL Server 2012 or SQL CLR C#

前端 未结 1 1252
清歌不尽
清歌不尽 2020-12-06 06:50

Is there a generally accepted standard way for sending HTTP requests from SQL Server 2012?

What I am trying to do is to use a remote server to process a search query

相关标签:
1条回答
  • 2020-12-06 07:28

    Like Joe suggested using HttpWebRequest instead of HttpClient works without having to use unsupported assemblies:

    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void SendRequest (string query, string table)
    {
        string address = "http://localhost:9000/api/search";
        HttpWebRequest request = (HttpWebRequest) WebRequest.Create(address);
        request.ContentType = "application/json; charset=utf-8";
        request.Method = "POST";
    
        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            string json = "{\"Query\":\""+query+"\",\"Table\":\""+table+"\"}";
    
            streamWriter.Write(json);
            streamWriter.Flush();
        }
    
        var httpResponse = (HttpWebResponse)request.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
        }
    }
    
    0 讨论(0)
提交回复
热议问题