C#后端发送一个url地址,获取这个地址的数据。
1 get方式
public string GetData(string url)
{
HttpClient client = new HttpClient();
var response = await client.GetAsync(url);
string result = "";
if (response.IsSuccessStatusCode)
{
Stream myResponseStream = response.Content.ReadAsStreamAsync().Result;
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
result = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
}
JObject jo = null;
string data=string.Empty;
if (!string.IsNullOrEmpty(result))
{
jo = (JObject)JsonConvert.DeserializeObject(result);
data=jo["list"].ToString();
}
return data;
}
2 post方式
public string GetData(string url)
{
try
{
#region 获取返回的数据
HttpClient client = new HttpClient();
JsonObj obj = new JsonObj();
obj.html = literatureInfo.Content;
string value = NewtonsoftJsonSerializer.Serialize(obj);
var content = new StringContent(value, Encoding.UTF8, "text/json");
HttpResponseMessage response = await client.PostAsync(url, content);
string result = "";
if