弄了一个post请求,在apipost中能正常返回结果,用apipost生成C#版(HttpClient)代码后,放到VS2017(.net版本是4.5)中运行,却没结果返回,换过几个版本的.net都不行,有人碰到过这个问题吗,就一个简单的demo程序,代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HttpClientDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async Task<string> postAsyncPro()
{
var client = new HttpClient();
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("http://rongheng.imwork.net:8083/smooth/global/boardArchiveInfo/add"),
Content = new StringContent("{\r\n \"identifier\":\"AD5634F5FD38ED32CBCD96B78465CD30\",\r\n \"boardArchiveId\":51,\r\n \"listMac\":[]\r\n}")
{
Headers =
{
ContentType = new MediaTypeHeaderValue("application/json")
}
}
};
Console.WriteLine("Post提交请求");
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
Console.WriteLine(body);
return body;
}
}
private async void btnHttpClitn_Click(object sender, EventArgs e)
{
try
{
string sRe = await postAsyncPro();
Console.WriteLine("Post结果:" + sRe);
}
catch(Exception ex)
{
Console.WriteLine("Post异常:"+ ex.Message);
}
}
}
}
我们也是第三方生成的代码库,可以看看是不是库匹配的型号
您说的using System.Net.Http这个库的问题吗?
执行get请求没问题,就是post请求怎么都没响应,用apipost就都没问题,折腾了好久没解决