using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace HttpClientQuery
{
class PageInfo
{
public int start; //得是public类型
public string table;
public int getStart(){
return start;
}
//不能用c#自带的get set方法 以下get set方法是在sts中建相应的java类后用自动生成方法得到的
public void setStart(int start){
this.start = start;
}
public string getTable(){
return table;
}
public void setTable(string table){
this.table = table;
}
public PageInfo(){
}
public PageInfo(int start, string table){
this.start = start;
this.table = table;
}
}
class FilterData
{
public string name;
public string value;
public string getName(){
return name;
}
public void setName(string name){
this.name = name;
}
public string getValue(){
return value;
}
public void setValue(string value){
this.value = value;
}
public FilterData()
{
}
public FilterData(string name, string value)
{
this.name = name;
this.value = value;
}
}
class DocQueryParam
{
public string id;
public List<FilterData> filterData;
public List<PageInfo> paginationInfo;
public int searchType;
public string tablename;
public string getId(){
return id;
}
public void setId(string id){
this.id = id;
}
public List<FilterData> getFilterData(){
return filterData;
}
public void setFilterData(List<FilterData> filterData){
this.filterData = filterData;
}
public List<PageInfo> getPaginationInfo(){
return paginationInfo;
}
public void setPaginationInfo(List<PageInfo> paginationInfo){
this.paginationInfo = paginationInfo;
}
public int getSearchType(){
return searchType;
}
public void setSearchType(int searchType){
this.searchType = searchType;
}
public string getTablename(){
return tablename;
}
public void setTablename(string tablename){
this.tablename = tablename;
}
public DocQueryParam(){
}
public DocQueryParam(string id, List<FilterData> filterData, List<PageInfo> paginationInfo, int searchType, string tablename){
this.id = id;
this.filterData = filterData;
this.paginationInfo = paginationInfo;
this.searchType = searchType;
this.tablename = tablename;
}
}
class FileInfo
{
public string fileId;
public string name;
public int fileSize;
public string fileType;
public int level;
public DateTime recordTime;
public string getFileId(){
return fileId;
}
public void setFileId(string fileId){
this.fileId = fileId;
}
public string getName(){
return name;
}
public void setName(string name){
this.name = name;
}
public int getFileSize(){
return fileSize;
}
public void setFileSize(int fileSize){
this.fileSize = fileSize;
}
public string getFileType(){
return fileType;
}
public void setFileType(string fileType){
this.fileType = fileType;
}
public int getLevel(){
return level;
}
public void setLevel(int level){
this.level = level;
}
public DateTime getRecordTime(){
return recordTime;
}
public void setRecordTime(DateTime recordTime){
this.recordTime = recordTime;
}
public FileInfo(){
}
public FileInfo(string fileId, string name, int fileSize, string fileType, int level, DateTime recordTime)
{
this.fileId = fileId;
this.name = name;
this.fileSize = fileSize;
this.fileType = fileType;
this.level = level;
this.recordTime = recordTime;
}
}
class DocQueryResult
{
public Boolean result;
public string errMessage;
public string queryId;
public List<FileInfo> queryFileList;
public List<PageInfo> paginationHistoryInfo;
public string IsEnd;
public Boolean getResult(){
return result;
}
public void setResult(Boolean result){
this.result = result;
}
public string getErrMessage(){
return errMessage;
}
public void setErrMessage(string errMessage){
this.errMessage = errMessage;
}
public string getQueryId(){
return queryId;
}
public void setQueryId(string queryId){
this.queryId = queryId;
}
public List<FileInfo> getQueryFileList(){
return queryFileList;
}
public void setQueryFileList(List<FileInfo> queryFileList){
this.queryFileList = queryFileList;
}
public List<PageInfo> getPaginationHistoryInfo(){
return paginationHistoryInfo;
}
public void setPaginationHistoryInfo(List<PageInfo> paginationHistoryInfo){
this.paginationHistoryInfo = paginationHistoryInfo;
}
public string getIsEnd(){
return IsEnd;
}
public void setIsEnd(string isEnd){
IsEnd = isEnd;
}
public DocQueryResult(){
}
public DocQueryResult(bool result, string errMessage, string queryId, List<FileInfo> queryFileList, List<PageInfo> paginationHistoryInfo, string isEnd)
{
this.result = result;
this.errMessage = errMessage;
this.queryId = queryId;
this.queryFileList = queryFileList;
this.paginationHistoryInfo = paginationHistoryInfo;
IsEnd = isEnd;
}
}
class DocQuery
{
public DocQuery()
{
}
/// <summary>
/// 使用get方法异步请求
/// </summary>
/// <param name="url">目标链接</param>
/// <returns>返回的字符串</returns>
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;
}
void test()
{
DocQueryParam docQueryParam = new DocQueryParam();
docQueryParam.setId("2311");
docQueryParam.setSearchType(2);
docQueryParam.setTablename("c_data");
List<FilterData> filterDataList = new List<FilterData>();
FilterData filterData = new FilterData();
filterData.setName("model");
filterData.setValue("L");
filterDataList.Add(filterData);
filterData = new FilterData();
filterData.setName("batch");
filterData.setValue("02");
filterDataList.Add(filterData);
docQueryParam.setFilterData(filterDataList);
List<PageInfo> pageInfoList = new List<PageInfo>();
PageInfo pageInfo = new PageInfo();
pageInfo.setStart(20);
pageInfo.setTable("c_data");
pageInfoList.Add(pageInfo);
docQueryParam.setPaginationInfo(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();
}
//给main方法加上async
static void Main(string[] args)
{
new DocQuery().test();
}
}
}