C# POST 및 Get 데이터

21780 단어 post
참조 DLL
 

일반 Get 데이터 및 포스트 데이터


 
 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
"; 67 } 68 returnData = strValue; 69 } 70 catch 71 { 72 return " "; 73 } 74 return returnData.Trim() + "
"; 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 }

첨부다른 포스트

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; }

        }

좋은 웹페이지 즐겨찾기