C# POST与Get数据

引用DLL

 

普通Get数据和Post数据

 

 1    public static string Get(string URL)

 2         {

 3             String ReCode = string.Empty;

 4             try

 5             {

 6                 HttpWebRequest wNetr = (HttpWebRequest)HttpWebRequest.Create(URL);

 7                 HttpWebResponse wNetp = (HttpWebResponse)wNetr.GetResponse();

 8                 wNetr.ContentType = "text/html";

 9                 wNetr.Method = "Get";

10                 Stream Streams = wNetp.GetResponseStream();

11                 StreamReader Reads = new StreamReader(Streams, Encoding.UTF8);

12                 ReCode = Reads.ReadToEnd();

13 

14                 //封闭临时不实用的资料 

15                 Reads.Dispose();

16                 Streams.Dispose();

17                 wNetp.Close();

18             }

19             catch (Exception ex) { throw ex; }

20 

21             return ReCode;

22 

23         }

24 

25         public static string Post(string url, string data)

26         {

27             string returnData = null;

28             try

29             {

30                 //byte[] buffer = Encoding.UTF8.GetBytes(data);

31                 //HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);

32                 //webReq.Method = "POST";

33                 //webReq.ContentType = "application/x-www-form-urlencoded";

34                 //webReq.ContentLength = buffer.Length;

35                 //Stream postData = webReq.GetRequestStream();

36                 //webReq.Timeout = 99999999;

37                 ////webReq.ReadWriteTimeout = 99999999;

38                 //postData.Write(buffer, 0, buffer.Length);

39                 //postData.Close();

40                 //HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();

41                 //Stream answer = webResp.GetResponseStream();

42                 //StreamReader answerData = new StreamReader(answer);

43                 //returnData = answerData.ReadToEnd();

44 

45                 string strURL = url;

46                 System.Net.HttpWebRequest request;

47                 request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);

48                 request.Method = "POST";

49                 request.ContentType = "application/json;charset=UTF-8";

50                 string paraUrlCoded = data;

51                 byte[] payload;

52                 payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);

53                 request.ContentLength = payload.Length;

54                 Stream writer = request.GetRequestStream();

55                 writer.Write(payload, 0, payload.Length);

56                 writer.Close();

57                 System.Net.HttpWebResponse response;

58                 response = (System.Net.HttpWebResponse)request.GetResponse();

59                 System.IO.Stream s;

60                 s = response.GetResponseStream();

61                 string StrDate = "";

62                 string strValue = "";

63                 StreamReader Reader = new StreamReader(s, Encoding.UTF8);

64                 while ((StrDate = Reader.ReadLine()) != null)

65                 {

66                     strValue += StrDate + "\r\n";

67                 }

68                 returnData = strValue;

69             }

70             catch

71             {

72                 return "获取错误";

73             }

74             return returnData.Trim() + "\n";

75         }

76         /// <summary>

77         /// 将json数据反序列化为Dictionary

78         /// </summary>

79         /// <param name="jsonData">json数据</param>

80         /// <returns></returns>

81         public static Dictionary<string, object> JsonToDictionary(string jsonData)

82         {

83             //实例化JavaScriptSerializer类的新实例。。。需要添加 System.Web.Extensions引用

84             JavaScriptSerializer jss = new JavaScriptSerializer();

85             try

86             {

87                 //将指定的 JSON 字符串转换为 Dictionary<string, object> 类型的对象

88                 return jss.Deserialize<Dictionary<string, object>>(jsonData);

89             }

90             catch (Exception ex)

91             {

92                 throw new Exception(ex.Message);

93             }

94         }

附.另外一个Post

public void Post()

{

 var d = new SendNews();

            d.touser = touser;

            d.agentid = "7";

            d.Description = wxmsg;



            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri("http://localhost:47870/");

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));



            HttpResponseMessage response = client.PostAsJsonAsync("api/SendMsg/SendNews", d).Result; 

}





   public class SendNews 

        {

            /// <summary>

            /// UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送 

            /// </summary>

            public string touser { get; set; }

            /// <summary>

            /// PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数

            /// </summary>

            public string toparty { get; set; }

            /// <summary>

            /// TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数 

            /// </summary>

            public string totag { get; set; }

            /// <summary>

            /// 消息类型

            /// </summary>

            public string msgtype { get; set; }

            /// <summary>

            /// 企业应用的id,整型。可在应用的设置页面查看 

            /// </summary>

            public string agentid { get; set; }



            /// <summary>

            /// 表示是否是保密消息,0表示否,1表示是,默认0 

            /// </summary>

            public string safe { get; set; }





            /// <summary>

            /// 文章标题

            /// </summary>

            public string Title { get; set; }

            /// <summary>

            /// 文章描述

            /// </summary>

            public string Description { get; set; }

            /// <summary>

            /// 图片链接,支持JPG、PNG格式,较好的效果为大图360*200,小图200*200

            /// </summary>

            public string PicUrl { get; set; }

            /// <summary>

            /// 点击图文消息跳转链接

            /// </summary>

            public string Url { get; set; }

        }

 

你可能感兴趣的:(post)