我在网上搜了很多别人都解决了的方法,但在这个网址就是不行,请各位高手帮忙看看能不能解决?
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;
        }

解决方案 »

  1.   

    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();
      

  2.   

    500错误一般原因是内部程序出错,可直接浏览器输入请求URL进行跟踪
      

  3.   

    地址是正确的,在浏览器里是正确的,(HttpWebResponse)req.GetResponse()时报错,2楼的方法我都试过了,还是报500的错,网址在我贴的代码里有,
      

  4.   

    用一个事件调用一下我贴上来的这个方法GetInfo(),看看提示就知道是什么错的。