C# 的问题:StartIndex 不能小于零。参数名称:startIndex

Problem with C#: StartIndex cannot be less than zero. Parameter name: 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 cannot be less than zero. Parameter name: 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;
    }
}

}

and 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*/
}