大佬教程收集整理的这篇文章主要介绍了C# 的问题:StartIndex 不能小于零。参数名称:startIndex,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的代码:
public static async voID Download()
{
InstagramDownloadMachine instagramDownloadMachine = new InstagramDownloadMachine();
await instagramDownloadMachine.Downloadtopath(@"https://www.insta.com/p/CLL_c2egcL9/","img.jpg");
}
当我执行它时,我收到此错误:
System.ArgumentOutOfRangeException: 'StartIndex 不能小于零。 参数名称:startIndex'
(顺便说一句,我使用此代码下载 insta 视频)
这是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;
}
}
}
和 IDk 是错误的 startindex /:
似乎异常来自 GetFileUrl 方法中的 html.Substring(auxIndex) 行。您正在使用的 Substring 方法重载将 startIndex 作为唯一参数,因此您的异常表示它不能为负。 auxIndex 变量的值为 -1,因为在您的 html 中找不到 fileKey。你应该在那里设置一个条件来处理这种情况。
更多解释:
IndexOf reference
Substring reference
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. */
您可以在方法内部进行检查以查看 auxIndex 是否为非负值,例如
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*/
}
以上是大佬教程为你收集整理的C# 的问题:StartIndex 不能小于零。参数名称:startIndex全部内容,希望文章能够帮你解决C# 的问题:StartIndex 不能小于零。参数名称:startIndex所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。