Tom20081 2018-08-13 11:47 采纳率: 100%
浏览 747
已采纳

C# 采集网页,浏览器可以打开,程序采集提示错误

网址:https://pgi.kaipanla.com/w12/share/PContent2.html?AID=1548&tag=2&atype=ord
浏览器打开可以正常显示内容,使用WebRequest无法获得网页内容。
请给出demo

  • 写回答

2条回答 默认 最新

  • threenewbee 2018-08-13 12:23
    关注
     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Net;
    
    namespace Q697477
    {
        class Program
        {
            public static string PostData(string url, string param)
            {
                string strURL = url;
                System.Net.HttpWebRequest request;
                request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                string paraUrlCoded = param;
                byte[] payload;
                payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
                request.ContentLength = payload.Length;
                Stream writer = request.GetRequestStream();
                writer.Write(payload, 0, payload.Length);
                writer.Close();
                System.Net.HttpWebResponse response;
                response = (System.Net.HttpWebResponse)request.GetResponse();
                System.IO.Stream s;
                s = response.GetResponseStream();
                string StrDate = "";
                string strValue = "";
                StreamReader Reader = new StreamReader(s, Encoding.UTF8);
                while ((StrDate = Reader.ReadLine()) != null)
                {
                    strValue += StrDate + "\r\n";
                }
                return strValue;
            }
            static void Main(string[] args)
            {
                string s = PostData("https://article.kaipanla.com/w1/api/index.php?apiv=w12&PhoneOSNew=3", "c=ForumsMsgJX&a=GetInfo&MsgID=1548&Tag=1");
                Console.WriteLine(s);
            }
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料