HttpWebRequest 远程服务器返回错误: (500) 内部服务器错误。

malin4 2011-01-08 06:02:38
我在网上搜了很多别人都解决了的方法,但在这个网址就是不行,请各位高手帮忙看看能不能解决?
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;
}
...全文
347 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
malin4 2011-01-09
  • 打赏
  • 举报
回复
没有高手来指点吗?
malin4 2011-01-08
  • 打赏
  • 举报
回复
用一个事件调用一下我贴上来的这个方法GetInfo(),看看提示就知道是什么错的。
malin4 2011-01-08
  • 打赏
  • 举报
回复
地址是正确的,在浏览器里是正确的,(HttpWebResponse)req.GetResponse()时报错,2楼的方法我都试过了,还是报500的错,网址在我贴的代码里有,
路人乙e 2011-01-08
  • 打赏
  • 举报
回复
500错误一般原因是内部程序出错,可直接浏览器输入请求URL进行跟踪
wuyq11 2011-01-08
  • 打赏
  • 举报
回复
500错误的原因是很多的
获取数据的地址是否正确,是否能打开
HTTP 监控软件里查看返回的状态
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(PageURL);
req.Method = "GET";
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream s = resp.GetResponseStream();
malin4 2011-01-08
  • 打赏
  • 举报
回复
自己先顶一下,在线等各位大侠的解答!

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧