1

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

Stefano Cavion
  • 641
  • 8
  • 16
  • Which line in your code results in that output اكل-الجوز-وعلاقته-وفوائده-للقلب-والامعاءhttps://Mywebsite.com/Post/3163/ – Clint Feb 27 '20 at 16:37
  • string FullMineURL= WebsiteURl+Server.UrlEncode(Localurl); –  Feb 27 '20 at 16:49
  • Is the TwitterShare some kind of library? Do you have the code that it uses? It looks like it does automatic translation or some kind of encoding before it outputs – Glubus Feb 27 '20 at 17:07
  • can you try inserting a break point `FullMineURL` and examine output, I doubt if the foreign characters are coming from there – Clint Feb 27 '20 at 17:09
  • TwitterShare is the id of the HTML anchor which take action to retweet it's not a library it's shown in the HTML in the code above –  Feb 27 '20 at 17:16
  • I already tried to insert breakpoint but it's working fine and the code comes as it should be the problem after I'm trying to share it –  Feb 27 '20 at 17:19
  • when you hover over the twitter share button, you still see the foreign character is that right ? – Clint Feb 27 '20 at 17:23
  • yeah is see full URL but the problem it's comes this way اكل-الجوز-وعلاقته-وفوائده-للقلب-والامعاءhttps://Mywebsite.com/Post/3163/ it's should be that way https://Mywebsite.com/Post/3163/اكل-الجوز-وعلاقته-وفوائده-للقلب-والامعاء –  Feb 27 '20 at 17:26
  • try adding this line in your formload `Form1.Action = Request.RawUrl;` – Clint Feb 27 '20 at 17:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208660/discussion-between-clint-and-darwiesh-mustafa). – Clint Feb 27 '20 at 17:53
  • you can use `bitly` to shorten the url and try it out http:/ / bit.ly/32z14Rd – Clint Feb 28 '20 at 08:56

1 Answers1

0

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

Clint
  • 6,011
  • 1
  • 21
  • 28
  • I'm getting null exception in this part of the code var response = await request.GetResponseAsync(); I checked the short link it's fine ,isn't it work in the localhost ? –  Feb 28 '20 at 15:12
  • An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll –  Feb 28 '20 at 15:28
  • @DarwieshMustafa [continue this discussion here](https://chat.stackoverflow.com/rooms/208660/discussion-between-clint-and-darwiesh-mustafa) – Clint Feb 28 '20 at 16:05