Get 和 Post 使用篇(1)

 

1.Post 请求发送方式

实例:

   const string sResponseEncoding = "gb2312";            

//测试文本信息            

string postText = "{\"touser\":\"OpenId\",\"msgtype\":\"text\",\"text\":{\"content\":\"Hello World\"}}";            

//测试图文信息            

string postNews = "{\"touser\":\"OpenId\",\"msgtype\":\"news\",\"news\":{\"articles\":[{\"title\":\"测试1\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"http://avatar.csdn.net/C/2/8/1_zhoufoxcn.jpg\"},{\"title\":\"测试2\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"http://avatar.csdn.net/C/2/8/1_zhoufoxcn.jpg\"}]}}";            

 const string url = "http;//URL";            

HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);            

webrequest.Method = "post";            

webrequest.Timeout = 10000;     //响应时间时间          

webrequest= "application/json;charset=UTF-8";   //传递格式           

webrequest.Credentials = CredentialCache.DefaultCredentials; //请求的身份验证信息为默认    

 

 string key = "My-Key";   //默认密钥            

 string timestamp = DateTime.Now.Ticks.ToString();//时间戳

webrequest.Headers.Add(“key”, key);

webrequest.Headers.Add(“timestamp”, timestamp);

//字符-字节-流

byte[] postdatabyte = Encoding.UTF8.GetBytes(postText);            

webrequest.ContentLength = postdatabyte.Length;            

Stream stream;            

stream = webrequest.GetRequestStream();            

stream.Write(postdatabyte, 0, postdatabyte.Length);           

  stream.Close();                     

Stream responseStream;            

try            

{  responseStream = webrequest.GetResponse().GetResponseStream();  }            

catch (Exception e) 

{  Console.WriteLine(string.Format("POST 操作发生异常:{0}", e.Message));   throw e;  }            

string stringResponse = string.Empty;            

using (StreamReader responseReader = new StreamReader(responseStream, Encoding.GetEncoding(sResponseEncoding)))            

     stringResponse = responseReader.ReadToEnd();

}            

responseStream.Close();

 Console.WriteLine(stringResponse);            

Console.ReadKey();

 

2.Get 请求方式(httpClient)

HttpClient httpClient = new HttpClient();            

string key = "My-Key";   //默认密钥   

 string timestamp = DateTime.Now.Ticks.ToString();            

 string url = “”;

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);            

request.Headers.Add(key, key);            

request.Headers.Add(timestamp, timestamp);           

HttpResponseMessage responseMessage = await httpClient.SendAsync(request);            

Console.WriteLine(await responseMessage.Content.ReadAsStringAsync());            

Console.ReadKey();

 

3.服务器接口

  public ActionResult ReceiveData(string jsonpcallback)

{

            var headers = HttpContext.Request.Headers.ToString();//获取headers 内容
            Stream s = System.Web.HttpContext.Current.Request.InputStream;
            byte[] receByte = new byte[s.Length];
            s.Read(receByte, 0, (int)s.Length);

           var receData = Encoding.UTF8.GetString(receByte);
           var JsReceData = Newtonsoft.Json.JsonConvert.DeserializeObject<WxReceive>(receData);

          //返回参数格式

        if (string.IsNullOrEmpty(jsonpcallback))
             return Json(QRTxt);
        else
            return JavaScript(string.Format("{0}({1});", jsonpcallback, Newtonsoft.Json.JsonConvert.SerializeObject(QRTxt)));

}

//声明实体类

    public class WxReceive
    {
        public string touser { get; set; }
        public string msgtype { get; set; }
    }

//微信图文消息实体类
    public class WxReceiveNews
    {
        public string touser { get; set; }
        public string msgtype { get; set; }
        public news news { get; set; }
    }
    public class news
    {
        public List<articles> articles { get; set; }
    }
    public class articles
    {
        public string title { get; set; }
        public string description { get; set; }
        public string url { get; set; }
        public string picurl { get; set; }
    }

4.其它

传递到服务器Base64,字符串进行URL解码: 

HttpUtility.UrlDecode(data.ToString().Trim());

 

参考文章和资源:

Post和Get 实例 http://blog.csdn.net/make1828/article/details/40649759

Post 提交讲解:https://imququ.com/post/four-ways-to-post-data-in-http.html

https://support.microsoft.com/zh-cn/kb/908573

http://www.cnblogs.com/ThomasNet/archive/2007/09/03/879331.html

http://www.cnblogs.com/netqq/p/5773846.html

http://bbs.csdn.net/topics/390820845/

http://blog.csdn.net/make1828/article/details/40649759

 

posted @ 2016-09-20 15:42  李寒星  阅读(350)  评论(0编辑  收藏  举报