柏虎资源网

专注编程学习,Python、Java、C++ 教程、案例及资源

WebForm程序调用webAPIf方法_winform调用webapi上传文件

1. 引用dll文件 RestSharp.dll Newtonsoft.Json.dll System.Web.Extensions.dll

using RestSharp;

using System.Web.Script.Serialization;

using Newtonsoft.Json.Linq;

private void button1_Click(object sender, EventArgs e)

{

List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

Dictionary<string, object> header = new Dictionary<string, object>();

header["SN"] = "TEST111";

JavaScriptSerializer jss = new JavaScriptSerializer();

jss.MaxJsonLength = System.Int32.MaxValue;

IRestResponse response = Post(jss.Serialize(header));

JObject jsonString = JObject.Parse(response.Content);

//string ss = HttpWebRequest_Get("http://172.17.9.210:8104/A225/A225.ashx");

//MessageBox.Show(ss);

}

IRestResponse Post(string body)

{

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

var client = new RestClient("http://IP地址:端口号/项目名称/文件名.ashx");

var request = new RestRequest(Method.POST);

request.AddParameter("undefined", body, ParameterType.RequestBody);

IRestResponse response = client.Execute(request);

return response;

}

public static string HttpWebRequest_Get(string url)

{

//ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);

Encoding encoding = Encoding.UTF8;

//构造一个Web请求的对象

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

request.Method = "GET";

request.Accept = "text/html, application/xhtml+xml, */*";

request.ContentType = "application/json";


//获取web请求的响应的内容

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))

{

return reader.ReadToEnd();

}

}

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言