本文链接:https://blog.csdn.net/weixin_40586592/article/details/85000822
本篇文章讨论C# 客户端,进行HTTP通信,适用JSON数据格式。
下边为GET和POST请求
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace wode.HTTP
{
class HttpUitls
{
public static string Get(string Url)
{
//System.GC.Collect();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Proxy = null;
request.KeepAlive = false;
request.Method = "GET";
//request.ContentType = "application/json; charset=UTF-8";
request.ContentType = "application/x-www-form-urlencoded";//窗体数据被编码为名称/值对形式
request.AutomaticDecompression = DecompressionMethods.GZip;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
if (response != null)
{
response.Close();
}
if (request != null)
{
request.Abort();
}
return retString;
}
/// <summary>
/// Post请求可用
/// </summary>
/// <param name="Url"></param>
/// <param name="Data"></param>
/// <param name="Referer"></param>
/// <returns></returns>
public static string Post(string Url, string Data, string Referer)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.Referer = Referer;
//request.Proxy = new WebProxy("192.168.1.12",80);
byte[] bytes = Encoding.UTF8.GetBytes(Data);
request.ContentType = "application/json; charset=UTF-8"; ;//窗体数据被编码为名称/值对形式
//request.ContentType = "application/json";
request.ContentLength = bytes.Length;
Stream myResponseStream = request.GetRequestStream();
myResponseStream.Write(bytes, 0, bytes.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
if (response != null)
{
response.Close();
}
if (request != null)
{
request.Abort();
}
return retString;
}
}
}
下边为前台界面进行调用方法,采用的是键值对形式
//使用POST请求服务器
var para = new Dictionary<string, string>();
para.Add("图标", haha);
string json = JsonConvert.SerializeObject(para);//序列化数据
string url = "http://192.168.9.9:8080/。。。。。";
string postJson = HttpUitls.Post(url, json, "");
//使用GET请求服务器
string baseUrl = "http://192.168.9.9.。。。。";
string getUrl = baseUrl + "?" + "au=" + au;
string getJson = HttpUitls.Get(getUrl);
如果想让接收的json形式比较直观,可以调用如下方法
/// <summary>
/// 格式化字符串,方便查看
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private string ConvertJsonString(string str)
{
JsonSerializer serializer = new JsonSerializer();
TextReader tr = new StringReader(str);
JsonTextReader jtr = new JsonTextReader(tr);
object obj = serializer.Deserialize(jtr);
if(obj!=null)
{
StringWriter textWriter = new StringWriter();
JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
{
Formatting = Formatting.Indented,
Indentation = 4,
IndentChar = ' '
};
serializer.Serialize(jsonWriter, obj);
return textWriter.ToString();
}
else
{
return str;
}
}
上述这些方法的调用必须添加Newtonsoft.Json命名空间。谢谢大家!