This is my code :
public static async void Download()
{
InstagramDownloadMachine instagramDownloadMachine = new InstagramDownloadMachine();
await instagramDownloadMachine.DownloadToPath(@"https://www.insta.com/p/CLL_c2egcL9/", "img.jpg");
}
When I execute it, I get this error:
System.ArgumentOutOfRangeException: 'StartIndex cannot be less than zero. Parameter name: startIndex'
(I use this code to download insta videos BTW)
So this is full-code of dll:
private String fileExtension, fileKey;
private const String imgExtension = ".jpg";
private const String videoExtension = ".mp4";
private const String videoKey = "video_url\": \"";
private const String imgKey = "display_url\": \"";
public async Task<Stream> Download(String url)
{
WebRequest request = WebRequest.Create(url);
var response = await request.GetResponseAsync();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
string fileUrl = GetFileUrl(responseFromServer);
var fileStream = GetFileStream(fileUrl);
return fileStream;
}
public async Task DownloadToPath(String url, String path)
{
var stream = await Download(url);
MemoryStream memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
File.WriteAllBytes(path, memoryStream.ToArray());
}
private Stream GetFileStream(String url)
{
HttpWebRequest fileRequest = (HttpWebRequest)WebRequest.Create(url);
var response = fileRequest.GetResponse();
return response.GetResponseStream();
}
private String GetFileUrl(String html)
{
String fileUrl = null;
if (html.Contains("video_url"))
{
fileExtension = videoExtension;
fileKey = videoKey;
}
else
{
fileExtension = imgExtension;
fileKey = imgKey;
}
var auxIndex = html.IndexOf(fileKey);
var partial = html.Substring(auxIndex);
var endOfUrl = partial.IndexOf(fileExtension) + fileExtension.Length;
fileUrl = html.Substring(auxIndex, endOfUrl);
fileUrl = fileUrl.Substring(fileKey.Length);
return fileUrl;
}
}
}
and idk that is wrong with startindex /:
Seems like the exception is coming from html.Substring(auxIndex) line inside GetFileUrl method. The Substring method overload you are using takes the startIndex as the only parameter hence your exception says it can't be negative. The auxIndex variable has value -1 because the fileKey was not found in your html. You should put a condition there to handle this scenario.
More explanation:
var auxIndex = html.IndexOf(fileKey);/* This method will return -1 if fileKey is not found in html. So auxIndex variable will have -1.*/
var partial = html.Substring(auxIndex); /* This Substring method expects the parameter (auxIndex) to be non-negative, So if the above IndexOf method returned -1, auxIndex will be -1 and that would cause the Substring method to throw error. */
You could have a check inside the method to see whether auxIndex is non-negative e.g.
if(auxIndex >= 0)
{
/* means your fileKey was found in the html, so you can put the rest of the code here*/
var partial = html.Substring(auxIndex);
//....other lines
}
else
{
/* you need to put logic to handle this case where fileKey was not found in html*/
}