C# 调用百度短链接api

百度短链接api

直接上码:

public static string PostDataGetHtml(string postDataStr)
        {
            string Url = "http://dwz.cn/create.php";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postDataStr.Length;
            Stream myRequestStream = request.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
            myStreamWriter.Write(postDataStr);
            myStreamWriter.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }

//调用
PostDataGetHtml("url="+postDataStr);

会得到返回的json 并转化成string。

 

posted on 2014-12-02 12:13  刘阿毛  阅读(1164)  评论(0编辑  收藏  举报

导航