سلام به همه دوستان
چطور میشه عنوان یه سایت رو به دست اورد . مثلا من ادرس یه صفحه html رو از داخل سیستمم به برنامه بدم و بعد برنامه بهم بگه بین تگه باز و بسته <title> چی نوشته شده. دوستان خواهش میکنم کمک کنید
0
سلام
اینو امتحان کن:
[CSHARP]
static void Main(string[] args)
{
string page = @"http://site.com/";
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(page);
StreamReader SR = new StreamReader(req.GetResponse().GetResponseStream());
Char[] buf = new Char[256];
int count = SR.Read(buf, 0, 256);
while (count > 0)
{
String outputData = new String(buf, 0, count);
Match match = Regex.Match(outputData, @"<title>([^<]+)", RegexOptions.IgnoreCase);
if (match.Success)
{
Console.WriteLine(match.Groups[1].Value);
}
count = SR.Read(buf, 0, 256);
}
}
[/CSHARP]
OR
[CSHARP]
[/CSHARPusing System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
namespace TestGetTitle
{
class Program
{
static void Main(string[] args)
{
String html = Request("http://www.google.dk", "GET");
String title = GetTitleFromHtml(html);
Console.WriteLine(title);
Console.ReadKey();
}
static String GetTitleFromHtml(String html)
{
return Regex.Match(html, "<title[^>]*?>(?<content>[^<]*?)</title>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups["content"].Value;
}
static String Request(String uri, String verb)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(uri);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = verb;
req.ContentLength = 0;
System.Net.WebResponse resp = req.GetResponse();
if (resp == null) return null;
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}
}
}
[/CSHARP]
0
Tashakor
:khodeshe:
سوال برنامه نویسی دارید؟
هیچ وقت در پرسیدن سوال شک نکنید