非英语的Twitter分享按钮

达维什·穆斯塔法

嘿,首先,谢谢大家阅读我的问题和您的时间。我有一个“ Twitter分享”按钮,

这是html代码:

<a id="TwitterShare"  target="_blank"   runat="server" rel="nofollow" class="twitter"><i class="fa fa-twitter "  aria-hidden="true">غرد</i></a>

这是背后的代码:

string RequestTwitter = "https://twitter.com/intent/tweet?text=";
string WebsiteURl = "https://www.MyWebsite.com/";
string Localurl = "Post/" + Postid + "/" + PostName;
string FullMineURL= WebsiteURl+Server.UrlEncode(Localurl);
string TwitterFinal = RequestTwitter + FullMineURL;
TwitterShare.Attributes.Add("href", TwitterFinal);

代码可以工作并获取URL,但是当我共享链接时,twitter不会读取完整的链接,这是因为以这种方式看到的链接

اكل-الجوز-وعلاقته-وفوائده-للقلب-والامعاءhttps://Mywebsite.com/Post/3163/

我尝试调试代码,但我发现代码以正确的方式出现

克林特

我跑你的代码,它工作得很好,我不认为它有问题,你的代码(也报告了这方面的一个类似的问题的问题

您可以使用URL Shortner来缩短url,并在您的tweet句柄中使用它,我尝试并能够共享tweet

您可以登录这里,并产生GenericAccessToken这里

_bitlyToken您可以生成的GenericAccessToken替代


用法

确保您的页面被标记为Async<%@ Page Async="true" %>

protected void Page_Load(object sender, EventArgs e)
{ 
 RegisterAsyncTask(new PageAsyncTask(CurrentPost); 
}
async Task CurrentPost()
{ 
...
var shortenURL =  await p.ShortenAsync(FullMineURL);
string TwitterFinal = RequestTwitter + shortenURL;
TwitterShare.Attributes.Add("href", TwitterFinal);
...
}

辅助方法

public async Task<string> ShortenAsync(string longUrl)
{
    //with thanks to @devfunkd - see https://stackoverflow.com/questions/31487902/nuget-package-for-bitly-to-shorten-the-links

    var url = string.Format("https://api-ssl.bitly.com/v3/shorten?access_token={0}&longUrl={1}",
            _bitlyToken, HttpUtility.UrlEncode(longUrl));

    var request = (HttpWebRequest)WebRequest.Create(url);
    try
    {
        var response = await request.GetResponseAsync();
        using (var responseStream = response.GetResponseStream())
        {
            var reader = new StreamReader(responseStream, Encoding.UTF8);
            var jsonResponse = JObject.Parse(await reader.ReadToEndAsync());
            var statusCode = jsonResponse["status_code"].Value<int>();
            if (statusCode == (int)HttpStatusCode.OK)
                return jsonResponse["data"]["url"].Value<string>();

            //else some sort of problem
            Console.WriteLine("Bitly request returned error code {0}, status text '{1}' on longUrl = {2}",
                statusCode, jsonResponse["status_txt"].Value<string>(), longUrl);
            //What to do if it goes wrong? I return the original long url
            return longUrl;
        }
    }
    catch (WebException ex)
    {
        var errorResponse = ex.Response;
        using (var responseStream = errorResponse.GetResponseStream())
        {
            var reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
            var errorText = reader.ReadToEnd();
            // log errorText
            Console.WriteLine("Bitly access threw an exception {0} on url {1}. Content = {2}", ex.Message, url, errorText);
        }
        //What to do if it goes wrong? I return the original long url
        return longUrl;
    }
} 

谢乔恩的回答

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档