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