用Post和Get方法调用接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using huahaocms.com.CORE;
using huahaocms.com.DbHelper;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
public partial class Test_jiekou : System.Web.UI.Page
{
    public string HTTP_URL = "http://fuwu.toyou.com.cn/mobile/v1/api/doc?";
    protected void Page_Load(object sender, EventArgs e)
    {
        List lstDocument = new List();
        int nodeId = 1;
        string json = this.GetJson(nodeId).ToString();
        //string json = @"{'code':'0000','message':'处理成功','userToken':null,'data':[{'id':'369','parentId':'1','text':'产品技术文档','resourceType':0,'path':'/resource/0'},{'id':370,'parentId':1,'text':'名词及术语','resourceType':0,'path':'/resource/0'},{'id':371,'parentId':1,'text':'存储运维常识','resourceType':0,'path':'/resource/0'},{'id':443,'parentId':1,'text':'配置指导','resourceType':0,'path':'/resource/0'},{'id':448,'parentId':1,'text':'玩转同有服务号','resourceType':1,'path':'http://fuwu.toyou.com.cn/group1/M00/00/05/anjwt1liSjaAWjz3AAmnoSsbpho242.pdf'}]}";

        JObject json1 = (JObject)JsonConvert.DeserializeObject(json);
        JArray array = (JArray)json1["data"];
        foreach (var jObject in array)
        {
            Document newDocument = new Document();
            newDocument.id =Convert.ToInt32(jObject["id"].ToString());
            newDocument.parentId = Convert.ToInt32(jObject["parentId"].ToString());
            newDocument.text =jObject["text"].ToString();
            newDocument.resourceType = jObject["resourceType"].ToString();
            newDocument.path = jObject["path"].ToString();
            lstDocument.Add(newDocument);
        }
        rpDocument.DataSource = lstDocument;
        rpDocument.DataBind();
    }
    /// 
    /// 文档对象
    /// 
    public class Document
    {
        public int id { get; set; }
        public int parentId { get; set; }
        public string text { get; set; }
        public string resourceType { get; set; }
        public string path { get; set; }
    }
    /// 
    /// 获取结果
    /// 
    /// 
    private object GetJson(int nodeId)
    {
        StringBuilder param = new StringBuilder();
        param.Append("nodeId=").Append(nodeId);
        String result = Get(HTTP_URL, param.ToString());
        return result;
    }
    /// 
    /// Get请求
    /// 
    /// 
    /// 
    public static string Get(string url, string paramData)
    {
        Stream respStream = null;
        HttpWebResponse httpResp = null;
        StreamReader respStreamReader = null;
        string res = null;
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            res = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            if (respStream != null)
            {
                respStream.Close();
            }

            if (httpResp != null)
            {
                httpResp.Close();
            }

            if (respStreamReader != null)
            {
                respStreamReader.Close();
            }
        }
        return res;
        // return strBuff.ToString();
    }
    /// 
    /// Post请求
    /// 
    /// 
    /// 
    public static string Post(string url, string paramData)
    {
        Stream respStream = null;
        HttpWebResponse httpResp = null;
        StreamReader respStreamReader = null;

        //StringBuilder strBuff = new StringBuilder();
        //int byteRead = 0;
        //int byteCount = 256;
        //char[] cbuffer = new char[byteCount];

        string res = null;
        try
        {
            byte[] byteArray = Encoding.UTF8.GetBytes(paramData); //转化
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
            httpReq.Method = "Post";
            httpReq.ContentType = "application/x-www-form-urlencoded";
            httpReq.ContentLength = byteArray.Length;
            respStream = httpReq.GetRequestStream();
            respStream.Write(byteArray, 0, byteArray.Length);//写入参数
            respStream.Close();
            httpResp = (HttpWebResponse)httpReq.GetResponse();
            respStreamReader = new StreamReader(httpResp.GetResponseStream(), Encoding.UTF8);
            res = respStreamReader.ReadToEnd();

        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            if (respStream != null)
            {
                respStream.Close();
            }

            if (httpResp != null)
            {
                httpResp.Close();
            }

            if (respStreamReader != null)
            {
                respStreamReader.Close();
            }
        }
        return res;
        // return strBuff.ToString();
    }
}

你可能感兴趣的:(C#,.NET)