post提交数据

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;

public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void sp_postman(string url,string json,out string result)
    {
        try
        {
            result = PostUrl(url, json);
        }
        catch(Exception ex)
        {
            result = ex.Message;
        }

        SqlContext.Pipe.Send(result);
    }


    public static string PostUrl(string url, string postData)
    {
        string result = "";

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

        req.Method = "POST";

        req.Timeout = 5000;

        req.ContentType = "application/json";

        byte[] data = Encoding.UTF8.GetBytes(postData);

        req.ContentLength = data.Length;

        using (Stream reqStream = req.GetRequestStream())
        {
            reqStream.Write(data, 0, data.Length);

            reqStream.Close();
        }

        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

        Stream stream = resp.GetResponseStream();

        //获取响应内容
        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
        {
            result = reader.ReadToEnd();
        }

        return result;
    }
}

猜你喜欢

转载自blog.csdn.net/mxbing1984/article/details/82499477