HttpWebRequest 远程服务器返回错误: (500) 内部服务器错误。
我在网上搜了很多别人都解决了的方法,但在这个网址就是不行,请各位高手帮忙看看能不能解决?
public bool GetInfo()
{
int i = 0;
string URL = "http://www.shfda.gov.cn:8080/sfda/table.do?page=";
string strCookie = "JSESSIONID=0729E915CB2F0A59E8A47681D83E408C,ASPSESSIONIDASTAQARC=HPIIJGOCDBKGIGHIBEJMAHGF";
string URLp;
string Encoding = "UTF-8";
bool ok = false;
Uri uriCookie;
HttpWebRequest hwReq;
HttpWebResponse hwRes;
StreamReader reader;
Regex r;
MatchCollection m;
ArrayList al = new ArrayList();
bool sign = false;
string strRegex;
SqlHelperD sqlHelperD = new SqlHelperD();
try
{
sqlHelperD.OpenConn();
for (i = 1; i < 369; i++)
{
m = null;
URLp = string.Empty;
URLp = URL + i + "&method=search&Id=2";
Uri uri = new Uri(URLp);
hwReq = (HttpWebRequest)WebRequest.Create(uri);
sign = hwReq.HaveResponse;
hwReq.Timeout = 120000;
hwReq.KeepAlive = false;
hwReq.ServicePoint.Expect100Continue = false;
hwReq.ServicePoint.UseNagleAlgorithm = false;
hwReq.ReadWriteTimeout = 120000;
hwReq.Method = "GET";
hwReq.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
hwReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
hwRes = (HttpWebResponse)hwReq.GetResponse();
hwReq.KeepAlive = false;
reader = new StreamReader(hwRes.GetResponseStream(), System.Text.Encoding.GetEncoding(Encoding));
strRegex = @"<td>\D*[\u4e00-\u9fa5]*\D*</td>|DBM_TABLE002\&id=\d*";
r = new Regex(strRegex, RegexOptions.IgnoreCase);
m = r.Matches(reader.ReadToEnd());
for (int j = 0; j < m.Count - 2; j = j + 2)
{
string strXuHao = m[j + 1].ToString();
strXuHao = strXuHao.Substring(16).Trim();
string URLd = string.Format("http://www.shfda.gov.cn:8080/sfda/table.do?method=showDetail&tableName=DBM_TABLE002&id={0}%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&tableId=2", strXuHao);
//URLp = string.Format("http://www.shfda.gov.cn:8080/sfda/table.do?method=showDetail&tableName=DBM_TABLE002&id={0}%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&tableId=2 {1}", strXuHao, DateTime.Now);
uri = new Uri(URLd);
hwReq = (HttpWebRequest)WebRequest.Create(uri);
hwReq.Timeout = 120000;
hwReq.KeepAlive = false;
hwReq.ServicePoint.Expect100Continue = false;
hwReq.ServicePoint.UseNagleAlgorithm = false;
hwReq.ReadWriteTimeout = 120000;
hwReq.Method = "GET";
hwReq.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
hwReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
hwReq.Referer = URLp;
try
{
hwRes = (HttpWebResponse)hwReq.GetResponse();
}
catch (WebException ex)
{
string strHtml = "";
HttpWebResponse res = ex.Response as HttpWebResponse;
if (res.StatusCode == HttpStatusCode.InternalServerError)
{
Stream s = res.GetResponseStream();
StreamReader objReader = new StreamReader(s, System.Text.Encoding.GetEncoding(Encoding));
strHtml = objReader.ReadToEnd();
objReader.Close();
}
else
{
strHtml = ex.Message;
}
}
sign = hwReq.HaveResponse;
hwReq.KeepAlive = true;
reader = new StreamReader(hwRes.GetResponseStream(), System.Text.Encoding.GetEncoding(Encoding));
strRegex = @"<td>\D*[\u4e00-\u9fa5]*\D*</td>";
r = new Regex(strRegex, RegexOptions.IgnoreCase);
string aaaa = reader.ReadToEnd();
m = r.Matches(reader.ReadToEnd());
}
}
}
catch (WebException ex)
{
MessageBox.Show(ex.Message);
return false;
}
finally
{
sqlHelperD.CloseConn();
}
MessageBox.Show("存储成功!");
return ok;
}