Hi,
I am trying to extract the host name from a HTTP header:
GET /1/1/typical.php HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, */*
Referer: http://www.httphost.com/1/1/
Accept-Language: en-us
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR
1.1.4322; .NET CLR 2.0.50727)
Host: www.httphost.com
Connection: Keep-AliveSo I just want to extract the text www.httphost.com
from the above. My code is below:
Regex r = new Regex(@"Host: ([A-Za-z0-9_:.]+)$", RegexOptions.Multiline);
// Match the regular expression pattern against a text string.
Match m = r.Match(message.ToString());
MessageBox.Show(m.Value);
But it shows a blank, I am sure there is an obvious answer.
Gilles Kohl [MVP] - 24 Mar 2008 22:00 GMT
>Hi,
>I am trying to extract the host name from a HTTP header:
[quoted text clipped - 19 lines]
>
>But it shows a blank, I am sure there is an obvious answer.
Hmm, can you post a complete snippet of code? (e.g. a self-contained method)
What is message, and what is its exact contents?
What does m.Success contain?
Regards,
Gilles.
schoensy - 24 Mar 2008 22:07 GMT
> Hi,
> I am trying to extract the host name from a HTTP header:
[quoted text clipped - 19 lines]
>
> But it shows a blank, I am sure there is an obvious answer.
I was able to validate that the following regexp works, but I haven't
run it through the code you wrote to see what the result is. Just
winging it, here's how I would write it out:
Match m = new Regex.Match(message.ToString(), @"Host: ([\w\.\-:_]*)");
MessageBox.Show(m.Value);
Hope that helps.
Darwin - 24 Mar 2008 23:04 GMT
Thanks for the help here's what I ended up with (appropriate error checks to
be implemented) :
Regex r = new Regex(@"Host: ([\w\.\-:_]*)", RegexOptions.Multiline);
Match m = r.Match(message.ToString());
if (m.Length > 0)
{
MessageBox.Show(m.Groups[1].Value);
}