大家好,又见面了,我是你们的朋友全栈君。
using System; using System.Net.Http; using System.Threading.Tasks; using System.Collections.Generic; using Newtonsoft.Json; using System.Net; using System.IO; using System.Text; using RestSharp;
namespace HttpClientQuery { class PageInfo { public int start { get; set; } //得是public类型
public string table { get; set; }
}
class FilterData { public string name { get; set; }
public string value { get; set; }
}
class DocQueryParam { public string id { get; set; }
public List<FilterData> filterData { get; set; }
public List<PageInfo> paginationInfo { get; set; }
public int searchType { get; set; }
public string tablename { get; set; }
}
class FileInfo { public string fileId { get; set; } public string name { get; set; } public int fileSize { get; set; } public string fileType { get; set; } public int level { get; set; } public DateTime recordTime { get; set; }
}
class DocQueryResult { public Boolean result { get; set; } public string errMessage { get; set; } public string queryId { get; set; } public List<FileInfo> queryFileList { get; set; } public List<PageInfo> paginationHistoryInfo { get; set; } public string IsEnd { get; set; }
}
class DocQuery {
public DocQuery() {
} /// <summary> /// 使用get方法异步请求 /// </summary> /// <param name=”url”>目标链接</param> /// <returns>返回的字符串</returns> public static async Task<string> postAsync(string url, String jsonStr) {
HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false });
HttpContent content = new StringContent(jsonStr); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(“application/json”);
HttpResponseMessage response = await client.PostAsync(url, content); response.EnsureSuccessStatusCode();//用来抛异常的 string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
return responseBody;
}
public static string post(string url, string postdata) { HttpWebResponse hw; string result = string.Empty; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = “POST”; req.ContentType = “application/json”; #region 添加Post 参数 byte[] data = Encoding.UTF8.GetBytes(postdata); req.ContentLength = data.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(data, 0, data.Length);//将post对象放入请求流中 reqStream.Close(); } #endregion try { HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream stream = resp.GetResponseStream(); //获取响应内容 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } } catch (WebException w) { hw = (HttpWebResponse)w.Response; StreamReader sr = new StreamReader(hw.GetResponseStream(), Encoding.UTF8, false); result = sr.ReadToEnd(); } return result; }
public static DocQueryResult restPost(string postdata, string url, string query) { RestSharp.RestClient client = new RestSharp.RestClient(url); RestRequest req = new RestRequest(query, Method.POST); req.RequestFormat = RestSharp.DataFormat.Json; //req.AddHeader(“cache-control”, “no-cache”);
req.AddJsonBody(postdata); //IRestResponse rsp = client.Execute(req); //return rsp.Content; IRestResponse<DocQueryResult> rsp = client.Execute<DocQueryResult>(req); DocQueryResult result = rsp.Data; return result; }
public void test() { DocQueryParam docQueryParam = new DocQueryParam(); docQueryParam.id = “2311”; docQueryParam.searchType = 2; docQueryParam.tablename = “c_data”;
List<FilterData> filterDataList = new List<FilterData>();
FilterData filterData = new FilterData(); filterData.name = “model”; filterData.value = “L”; filterDataList.Add(filterData);
filterData = new FilterData(); filterData.name = “batch”; filterData.value = “02”; filterDataList.Add(filterData);
docQueryParam.filterData = filterDataList;
List<PageInfo> pageInfoList = new List<PageInfo>();
PageInfo pageInfo = new PageInfo(); pageInfo.start = 20; pageInfo.table = “c_data”; pageInfoList.Add(pageInfo);
docQueryParam.paginationInfo = pageInfoList;
//序列化 //var jsonStr = JsonConvert.SerializeObject(docQueryParam); //Console.WriteLine(“docQuery json str: ” jsonStr);
string url = “http://192.168.1.100:8080/archive/archiveQuery”;
var ret = DocQuery.postAsync(url, jsonStr); //给调用该方法的方法加上async //DocQueryResult docQueryResult = JsonConvert.DeserializeObject<DocQueryResult>(ret); //var docQueryResult = JsonConvert.DeserializeObject<DocQueryResult>(ret.Result); //string isend = docQueryResult.getIsEnd();
Console.ReadKey();
}
} }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。