679
社区成员
发帖
与我相关
我的任务
分享
var ridecost = {
DERIVENO: $("input[name='DERIVENO']").val(),
PASSENGER_QTY: $("input[name='PASSENGER_QTY']").val(),
DISTANCE: $("input[name='DISTANCE']").val(),
KMCOST: $("input[name='KMCOST']").val(),
};
$.ajax({
type: "post",
url: "/SCRP0103/SaveData",
data: { ridecost: JSON.stringify(ridecost) },
webapiURL += "/api/orglevel/get?";
WebAPIRequest request1 = new WebAPIRequest();
string jsonData1 = request1.Get(webapiURL);
JavaScriptSerializer serializerl = new JavaScriptSerializer();
List<EMP_ORG_LV> rst1 = serializerl.Deserialize<List<EMP_ORG_LV>>(jsonData1);
private void button1_Click(object sender, EventArgs e)
{
string ss= HttpPost("http://localhost:41558/api/Demo/PostXXX", "{Code:\"test089\",Name:\"test1\"}");
}
public static string HttpPost(string url, string body)
{
//ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/json";
byte[] buffer = encoding.GetBytes(body);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
string ss= HttpPost("我的链接?", "参数?");ss是api的返回结果?
/// <summary>
/// HttpClient实现Post请求(异步)
/// </summary>
static async void dooPost()
{
string url = "http://localhost:52824/api/register";
//设置HttpClientHandler的AutomaticDecompression
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
//创建HttpClient(注意传入HttpClientHandler)
using (var http = new HttpClient(handler))
{
//使用FormUrlEncodedContent做HttpContent
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{ {"Id","6"},
{"Name","添加zzl"},
{"Info", "添加动作"}//键名必须为空
});
//await异步等待回应
var response = await http.PostAsync(url, content);
//确保HTTP成功状态值
response.EnsureSuccessStatusCode();
//await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
网上搜的方法
/// <summary>
/// HttpClient实现Post请求(异步)
/// </summary>
public async void dooPost()
{
IList<SendCarCost_KM> rideCostInfo = ModelConvertHelper<SendCarCost_KM>.ConvertToModel(_rideCostRepository.GetSendCarCost());
for (int i = 0; i < rideCostInfo.Count; i++)
{
string url = "http://121.12.249.239:8000/QueryMileage";
//设置HttpClientHandler的AutomaticDecompression
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
//创建HttpClient(注意传入HttpClientHandler)
using (var http = new HttpClient(handler))
{
//使用FormUrlEncodedContent做HttpContent
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{ {"regNO","6"},
{"begin","添加zzl"},
{"end", "添加动作"}
});
//await异步等待回应
var response = await http.PostAsync(url, content);
//确保HTTP成功状态值
response.EnsureSuccessStatusCode();
//await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
string aaa = await response.Content.ReadAsStringAsync();
}
}
}
改了下,进不去方法,直接报错
{"非同步作業目前無法開始。非同步作業只有在非同步處理常式或模組或是頁面生命週期中特定事件期間中才能開始。如果執行頁面時發生此例外狀況,請確認頁面已標示為 <%@ Page Async=\"true\" %>。此例外狀況也可能表示嘗試呼叫一般在 ASP.NET 要求處理中不支援的 \"async void\" 方法。相反地,非同步方法應傳回工作而呼叫端應等候它。"}