C# HTTP JSON

没玩过C#,现在需要搞一个pc端的简单应用,链接到云端。在友人的帮助下参看http://blog.csdn.net/huwei2003/article/details/47277457 WebAPI文章,虽然不是我想要的,但是文章中提到

在MVC中大家都知道,获取Request和Response使用HttpRequest和HttpResponse两个类,在WebAPI中使用另外两个类:

HttpRequestMessage 和HttpResponseMessage,分别用于封装Requset和Response。

关键字:HttpRequest和HttpResponse,所以有了如下代码测试:

接受到的数据,需要通过JSON序列化为结构:

    public class objdata {
        public string paramMemberId{get;set;}
        public string resultMemberId { get; set; }
    }
    
    public class JsonClass
    {
        public int status { get; set; }
        public string msg { get; set; }
        public objdata data { get; set; }
        
    }

测试通过点击按键,执行

 public string myWebRequest( string url, string postData)
        {
            HttpWebRequest webRequest = null;
            StreamWriter requestWriter = null;
            string responseData = "";
            webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
            //webRequest.Method = method.ToString();
            webRequest.ServicePoint.Expect100Continue = false;


            webRequest.KeepAlive = false;
            webRequest.ProtocolVersion = HttpVersion.Version10;


            webRequest.ContentType = "application/json";
                    webRequest.Method = "POST";
                //POST the data.
                requestWriter = new StreamWriter(webRequest.GetRequestStream());
                try
                {
                    requestWriter.Write(postData);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    requestWriter.Close();
                    requestWriter = null;
                }
            
            responseData = myWebResponseGet(webRequest);
            webRequest = null;


            var serializer = new JavaScriptSerializer();
           // var serializedResult = serializer.Serialize(responseData);


            JsonClass list = serializer.Deserialize(responseData);
            string result = list.msg;


            return responseData;
        }
        public string myWebResponseGet(HttpWebRequest webRequest)
        {
            StreamReader responseReader = null;
            string responseData = "";
            try
            {
                responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
                responseData = responseReader.ReadToEnd();
            }
            catch
            {
                throw;
            }
            finally
            {
                webRequest.GetResponse().GetResponseStream().Close();
                responseReader.Close();
                responseReader = null;
            }
            return responseData;
        }

此处引用的文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


using System.Net;
using System.IO;
using System.Web;


using System.Web.Script;
using System.Web.Script.Serialization;
以及添加引用

C# HTTP JSON_第1张图片


你可能感兴趣的:(其他)