C#的问题:StartIndex不能小于零。参数名称:startIndex
这是我的代码:C#的问题:StartIndex不能小于零。参数名称:startIndex,c#,instagram,C#,Instagram,这是我的代码: public static async void Download() { InstagramDownloadMachine instagramDownloadMachine = new InstagramDownloadMachine(); await instagramDownloadMachine.DownloadToPath(@"https://www.insta.com/p/CLL_c2egcL9/", "img.jpg&q
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;
}
}
私有字符串fileExtension,fileKey;
私有常量字符串imgExtension=“.jpg”;
private const String videoExtension=“.mp4”;
private const String videoKey=“video\u url\”:\”;
私有常量字符串imgKey=“显示url\”:\”;
公共异步任务下载(字符串url)
{
WebRequest=WebRequest.Create(url);
var response=wait request.GetResponseAsync();
Stream dataStream=response.GetResponseStream();
StreamReader=新的StreamReader(数据流);
字符串responseFromServer=reader.ReadToEnd();
字符串fileUrl=GetFileUrl(responseFromServer);
var fileStream=GetFileStream(fileUrl);
返回文件流;
}
公共异步任务下载路径(字符串url、字符串路径)
{
var stream=等待下载(url);
MemoryStream MemoryStream=新的MemoryStream();
stream.CopyTo(memoryStream);
writealBytes(路径,memoryStream.ToArray());
}
私有流GetFileStream(字符串url)
{
HttpWebRequest fileRequest=(HttpWebRequest)WebRequest.Create(url);
var response=fileRequest.GetResponse();
返回response.GetResponseStream();
}
私有字符串GetFileUrl(字符串html)
{
字符串fileUrl=null;
if(html.Contains(“视频url”))
{
fileExtension=videoExtension;
fileKey=videoKey;
}
其他的
{
fileExtension=imgExtension;
fileKey=imgKey;
}
var auxIndex=html.IndexOf(fileKey);
var partial=html.Substring(生长素指数);
var endOfUrl=partial.IndexOf(fileExtension)+fileExtension.Length;
fileUrl=html.Substring(auxIndex,endofur);
fileUrl=fileUrl.Substring(fileKey.Length);
返回fileUrl;
}
}
}
startindex/:错误的idk似乎来自GetFileUrl方法内的html.Substring(auxIndex)行。您正在使用的Substring方法重载将startIndex作为唯一的参数,因此您的异常表示它不能为负。auxIndex变量的值为-1,因为在html中找不到fileKey。你应该在那里设置一个条件来处理这个场景 更多说明: 你可以在方法内部检查生长素指数是否为非阴性
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*/
}
还有一件事,可以将其用于xamarin.forms吗?(具有外部.dll组件;我假定此错误是在DownloadToPath()中引发的)方法。如果它在DLL中,那么它将很难解决。如果它不在DLL中,那么您需要为它发布代码,以便我们能够提供帮助。异常的完整堆栈跟踪是什么?完整代码添加了一个旁白,如果可能的话,使用HttpClient而不是手动构建WebRequests。稍后您将感谢自己。也没有只需复制到内存流,然后将所有字节写入文件。打开一个文件流并复制到该文件流,而不是重复工作。我不明白,你能解释更多吗?我编辑了我的答案,并提供了更多解释。希望能有所帮助
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*/
}