正则表达式判断手机号码属于哪个运营商
#移动号段 1340-1348|135-139|147|150-152|1571-1572|1574|158-159|182|183|187|188
^1(34[0-8]|(3[5-9]|47|5[0-2]|57[124]|5[89]|8[2378])\\d)\\d{7}$
#联通号段 130|131|132|145|155|156|185|186
^1(3[0-2]|45|5[56]|8[56])\\d{8}$
#电信号段 133|153|180|189
^1(33|53|8[09])\\d{8}$
/// <summary>
/// 返回手机号的运营商,1移动 2联通 3电信
/// </summary>
/// <param name="mobile"></param>
/// <returns></returns>
public static int getMobileType(string mobile)
{
if (!string.IsNullOrWhiteSpace(mobile))
{
string ydExp = "^1(34[0-8]|(3[5-9]|47|5[0-2]|57[124]|5[89]|8[2378])\\d)\\d{7}$";
string ltExp = "^1(3[0-2]|45|5[56]|8[56])\\d{8}$";
string dxExp = "^1(33|53|8[09])\\d{8}$";
Regex reg = new Regex(ydExp);
if (reg.IsMatch(mobile))
{
return 1;
}
reg = new Regex(ltExp);
if (reg.IsMatch(mobile))
{
return 2;
}
reg = new Regex(dxExp);
if (reg.IsMatch(mobile))
{
return 3;
}
}
return 0;
}
//GET方式发送得结果
private static String doGetRequest(string url)
{
HttpWebRequest hwRequest;
HttpWebResponse hwResponse;
string strResult = string.Empty;
try
{
hwRequest = (System.Net.HttpWebRequest)WebRequest.Create(url);
hwRequest.Timeout = 5000;
hwRequest.Method = "GET";
hwRequest.ContentType = "application/x-www-form-urlencoded";
}
catch (System.Exception err)
{
WriteErrLog(err.ToString());
return strResult;
}
//get response
try
{
hwResponse = (HttpWebResponse)hwRequest.GetResponse();
StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.ASCII);
strResult = srReader.ReadToEnd();
srReader.Close();
hwResponse.Close();
}
catch (System.Exception err)
{
WriteErrLog(err.ToString());
}
return strResult;
}
Post调用实例:
string param = string.Format("spidxxx={0}&sppasooosword={1}&dc=15&dassssm={2}", spid, sppw, content);
byte[] bTemp = Encoding.ASCII.GetBytes(param);
result = doPostRequest(url, bTemp);
//POST方式发送得结果
public static String doPostRequest(string url, byte[] bData)
{
System.Net.HttpWebRequest hwRequest;
System.Net.HttpWebResponse hwResponse;
string strResult = string.Empty;
if (string.IsNullOrWhiteSpace(url) || bData == null)
{
return strResult;
}
try
{
hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
hwRequest.Timeout = 5000;
hwRequest.Method = "POST";
hwRequest.ContentType = "application/x-www-form-urlencoded";
hwRequest.ContentLength = bData.Length;
System.IO.Stream smWrite = hwRequest.GetRequestStream();
smWrite.Write(bData, 0, bData.Length);
smWrite.Close();
}
catch (System.Exception err)
{
return "短信调用异常1,exception = " + err.ToString();
}
//get response
try
{
hwResponse = (HttpWebResponse)hwRequest.GetResponse();
StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.ASCII);
strResult = srReader.ReadToEnd();
srReader.Close();
hwResponse.Close();
}
catch (System.Exception err)
{
return "短信调用异常2,exception = " + err.ToString();
}
return strResult;
}
//字符编码成HEX
private static String encodeHexStr(int dataCoding, String realStr)
{
string strhex = "";
try
{
Byte[] bytSource = null;
if (dataCoding == 15)
{
bytSource = Encoding.GetEncoding("GBK").GetBytes(realStr);
}
else if (dataCoding == 8)
{
bytSource = Encoding.BigEndianUnicode.GetBytes(realStr);
}
else
{
bytSource = Encoding.ASCII.GetBytes(realStr);
}
for (int i = 0; i < bytSource.Length; i++)
{
strhex = strhex + bytSource[i].ToString("X2");
}
}
catch (System.Exception err)
{
WriteErrLog(err.ToString());
}
return strhex;
}
//hex编码还原成字符
private static String decodeHexStr(int dataCoding, String hexStr)
{
String strReturn = "";
try
{
int len = hexStr.Length / 2;
byte[] bytSrc = new byte[len];
for (int i = 0; i < len; i++)
{
string s = hexStr.Substring(i * 2, 2);
bytSrc[i] = Byte.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier);
}
if (dataCoding == 15)
{
strReturn = Encoding.GetEncoding("GBK").GetString(bytSrc);
}
else if (dataCoding == 8)
{
strReturn = Encoding.BigEndianUnicode.GetString(bytSrc);
}
else
{
strReturn = System.Text.ASCIIEncoding.ASCII.GetString(bytSrc);
}
}
catch (System.Exception err)
{
WriteErrLog(err.ToString());
}
return strReturn;
}