C#模拟HTTP请求Post JSON

2021-06-22 01:03

阅读:444

标签:模拟   https   adt   writer   开发   sts   tst   stream   new   

  前言

  因为接口是http的,我们站点是https的,不能https直接ajax请求http,所以需要在SharePoint中开发一个模拟请求Ajax的Service,分享一下。

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    string json = "{\"user\":\"test\"," +
                  "\"password\":\"bla\"}";

    streamWriter.Write(json);
    streamWriter.Flush();
    streamWriter.Close();
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
}

 

C#模拟HTTP请求Post JSON

标签:模拟   https   adt   writer   开发   sts   tst   stream   new   

原文地址:https://www.cnblogs.com/jianyus/p/10101867.html


评论


亲,登录后才可以留言!