httpclient post

本文介绍了如何使用 C# 的 HttpClient 进行API请求,包括构造DocQueryParam对象,设置过滤条件、分页参数,并展示了如何序列化和解析JSON响应。重点在于使用PageInfo和FilterData类来处理查询结果的分页和筛选。
摘要由CSDN通过智能技术生成

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();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值