C#中的用正则表达式取出字符串中特定的字符:
说明:
inputstr--原字符串
pat--正则表达式
itemid--要取出的项目序号
addsplit--是否在每项后添加分隔符
![程序代码](http://www.lingye.net/blog/images/code.gif)
public string GetStr(string inputstr, string pat, int itemid, int addsplit)
{
Regex r = new Regex(pat);
Match m = r.Match(inputstr);
string tempstr = "";
if (m.Success)
{
foreach (Match ms in Regex.Matches(inputstr, pat))
{
if (itemid != 0)
{
if (addsplit == 0)
{
tempstr += ms.Groups[itemid].ToString();
}
else {
tempstr += ms.Groups[itemid].ToString() + "|";
}
}
else {
if (addsplit == 0)
{
tempstr += ms.ToString();
}
else
{
tempstr += ms.ToString() + "|";
}
}
}
return tempstr;
}
else {
return "";
}
}
C#中用来执行POST请求的代码
说明:
PostUrl--POST地址
PostData--POST的数据
![程序代码](http://www.lingye.net/blog/images/code.gif)
public string GetPost(string PostUrl,string PostData)
{
byte[] bytes = Encoding.ASCII.GetBytes(PostData);
try
{
WebRequest wr = WebRequest.Create(PostUrl);
wr.Method = "POST";
wr.ContentType="application/x-www-form-urlencoded";
wr.Headers.Add("Cache-Control","no-cache");
//wr.Headers.Add("Referer",RefererUrl);
wr.ContentLength = bytes.Length;
Stream oStreamOut = wr.GetRequestStream();
oStreamOut.Write(bytes, 0, bytes.Length);
oStreamOut.Close();
WebResponse wresp = wr.GetResponse();
Stream outstr = wresp.GetResponseStream();
StreamReader outstrre = new StreamReader(outstr, Encoding.GetEncoding("gb2312"));
string respHTML = outstrre.ReadToEnd();
wresp.Close();
return respHTML;
}
catch (WebException ex)
{
return ex.ToString();
}
}