c#查找PHP变量从URL代码
本文关键字:URL 代码 变量 查找 PHP | 更新日期: 2023-09-27 18:04:04
在c#中,我想从URL代码中找到一个PHP变量
示例:我想从www.domain.com/file.php?var=text
中找出text
这样做的主要原因是我想解析
的输出youtube.com/get_video_info?video_id=
或System.IO.Path.GetTempPath() + @"'tempytinfo.txt"
,格式为:?var=value&var2=value&var3=value&
…
我的努力试图找出这个坐在谷歌,调试其他答案,要么不工作,要么只是不是我要找的,这是我迄今为止的努力(也在第一个awnser的帮助下):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Paste A YouTube URL/URI to Download:");
string yturlstr = Console.ReadLine();
Uri yturl = new Uri(yturlstr);
Console.WriteLine("Finding ID in YouTube URL...");
string ytid = yturlstr.Split('?')[1];
ytid = ytid.Replace("v=", "");
Console.WriteLine("Found Watch ID.");
Console.WriteLine("The " + '"' + "Watch ID" + '"' + " is " + ytid);
Console.WriteLine("Getting Video Info...");
Download("http://youtube.com/get_video_info?video_id=" + ytid, System.IO.Path.GetTempPath() + @"'tempytinfo.txt");
Console.WriteLine("Got Info.");
Console.WriteLine("Reading Video Info...");
string viddata = System.IO.File.ReadAllText(System.IO.Path.GetTempPath() + @"'tempytinfo.txt");
Console.WriteLine("Parsing Video Info...");
Dictionary<string, string> videodata = new Dictionary<string, string>();
string vdt = viddata;
string[] vdata = vdt.Split('&');
foreach (char param in vdt)
{
string[] paramParts = char.ToString(param).Split('=');
videodata.Add(paramParts[0], paramParts[1]);
}
string uefsm = WebUtility.UrlDecode(videodata["url_encoded_fmt_stream_map"]);
Dictionary<string, string> videosubdata = new Dictionary<string, string>();
string[] vsdata = uefsm.Split('&');
foreach (char param2 in uefsm)
{
string[] paramParts2 = char.ToString(param2).Split('=');
videosubdata.Add(paramParts2[0], paramParts2[1]);
}
Uri downloadlink = new Uri(WebUtility.UrlDecode(videosubdata["url"]));
Console.WriteLine("Download Link: " + '"' + downloadlink + '"');
Console.ReadKey();
}
private static void Download(string URL, string WhereToDownload)
{
var client = new WebClient();
Uri downloadurl = new Uri(URL);
client.DownloadFile(downloadurl, WhereToDownload);
}
}
}
,我的错误是:
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in ConsoleApplication2.exe
Additional information: Index was outside the bounds of the array.
我会解析所有参数并将它们存储在一个集合中以便于访问它们。假设您在yturl
中有URL,就像您在代码中所做的那样,您可以创建集合:
Dictionary<string, string> parameters = new Dictionary<string, string>();
string tmpStr = yturl.Split('?')[1];
string[] params = tmpStr.Split('&');
foreach (string param in params)
{
string[] paramParts = param.Split('=');
parameters.Add(paramParts[0], paramParts[1]);
}
然后像你的例子一样,用:
获取"text"string var = parameters["var"];
// var == "text"
和其他参数的名称:
string var1 = parameters["var1"];
string var2 = parameters["var2"];