Hey guys in first place I would thank all of you for reading my question and for your time. I have a "Twitter Share" button,
This is the html code:
<a id="TwitterShare" target="_blank" runat="server" rel="nofollow" class="twitter"><i class="fa fa-twitter " aria-hidden="true">غرد</i></a>
And this is the code behind:
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);
The code work and get the url, but when I share the link twitter isn't reading the full link, and this is beacuse the link it's seen in this way
اكل-الجوز-وعلاقته-وفوائده-للقلب-والامعاءhttps://Mywebsite.com/Post/3163/
I have tried to debug the code but I've find out the code come by the right way
I ran your code and it works fine, I don't think its a problem with your code (A similar problem was also reported in this question
You can use URL Shortner to shorten url and use this in your tweet handle, I tried and was able to share the tweet
You can sign up here and generate GenericAccessToken here
Substitute _bitlyToken
with GenericAccessToken which you can generate
Usage
Ensure your Page is marked as 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);
...
}
Helper Method
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;
}
}
Thanks to Jon's Answer
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.