用Post和Get方法调用接口

11 篇文章 0 订阅
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<Document> lstDocument = new List<Document>();
        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();
    }
    /// <summary>
    /// 文档对象
    /// </summary>
    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; }
    }
    /// <summary>
    /// 获取结果
    /// </summary>
    /// <returns></returns>
    private object GetJson(int nodeId)
    {
        StringBuilder param = new StringBuilder();
        param.Append("nodeId=").Append(nodeId);
        String result = Get(HTTP_URL, param.ToString());
        return result;
    }
    /// <summary>
    /// Get请求
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    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();
    }
    /// <summary>
    /// Post请求
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值