Short URL code can also be generated using the Baidu API

Source: Internet
Author: User

Case: After the customer sign, the system will reply to a text message to the customer and a review service link, because the link has long, SMS will be automatically truncated, so the use of short-chain way to send.

The code is as follows:

public string Gettinyurl (string strlongurl)
{
Try
{
byte[] PostData = Encoding.UTF8.GetBytes ("url=" + strlongurl);
var url = "http://dwz.cn/create.php";
var client = new WebClient ();
Client. Headers.add ("Content-type", "application/x-www-form-urlencoded");
byte[] ResponseData = client. Uploaddata (URL, "POST", postdata);
var result = Encoding.UTF8.GetString (responsedata);

idictionary<string, string> dicresult = Parsesinge (result);
return dicresult["TinyURL"]. Replace ("\ \", "");
}
catch (Exception)
{
Return "";
}
}

<summary>
Parsing JSON strings
</summary>
<param name= "Pjsonstr" > string to parse such as: [{...,...,...}] </param>
<returns> Return Data collection </returns>
Public idictionary<string, String> parsesinge (string pjsonstr)
{
if (Pjsonstr.trim (). Length = = 0 | | Pjsonstr.indexof ("{") = =-1 | | Pjsonstr.indexof ("}") = =-1) return null;

System.Text.StringBuilder sb = new System.Text.StringBuilder (PJSONSTR);
Remove [{and}]
Sb. Remove (0, 1);
Sb. Remove (sb.) Length-1, 1);
string[] arr = sb. ToString (). Split (new string[] {","}, Stringsplitoptions.removeemptyentries);
if (arr = = NULL | | arr. Length = = 0) return null;

Resolving key-value pairs
dictionary<string, string> dic = new dictionary<string, string> ();
string[] Brr = null;
foreach (String str in arr)
{
BRR = str. Split (new char[] {': '}, 2);
Dic. ADD (Brr[0]. Replace ("\" "," "), Brr[1]. Replace ("\" "," "));
}

return dic;
}

The above is to generate short URL code, we hope to help. More. NET Tutorials: net.itcast.cn/net/video.shtml

This article is from the "World of Books" blog, please be sure to keep this source http://10221941.blog.51cto.com/10211941/1650053

Short URL code can also be generated using the Baidu API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.