当前位置: 移动技术网 > IT编程>开发语言>.net > 解决iis7.5服务器上.net 获取不到https页面的信息

解决iis7.5服务器上.net 获取不到https页面的信息

2017年12月12日  | 移动技术网IT编程  | 我要评论

我的获取页面需要cookie,不需要的可以去掉;

get的方法:

复制代码 代码如下:

/// <summary>
        /// 获取url访问的html内容 获取https 页面的
        /// </summary>
        /// <param name="url">url地址</param>
        /// <returns>html内容</returns>
        public static string getwebcontent(string url, cookiecontainer cookiecontainer)
        {
            string strresult = "";
            try
            {
                servicepointmanager.expect100continue = true;
                servicepointmanager.securityprotocol = securityprotocoltype.ssl3;
                httpwebrequest request = (httpwebrequest)webrequest.create(url);
                request.cookiecontainer = cookiecontainer;
                request.timeout = 30000;
                request.headers.set("pragma", "no-cache");

                httpwebresponse response = (httpwebresponse)request.getresponse();
                stream streamreceive = response.getresponsestream();

                encoding encoding = encoding.getencoding("utf-8");
                streamreader streamreader = new streamreader(streamreceive, encoding);
                strresult = streamreader.readtoend();
            }
            catch
            {

            }
            return strresult;
        }

 post的方法:

复制代码 代码如下:

/// <summary>
        /// post提交数据到https
        /// </summary>
        /// <param name="posturl"></param>
        /// <param name="postdata"></param>
        /// <param name="header"></param>
        /// <param name="cookiecontainer"></param>
        /// <returns></returns>
        public static string setposthtml(string posturl, string postdata, httpheader header, cookiecontainer cookiecontainer)
        {
            string restr = "";
            servicepointmanager.expect100continue = true;
            servicepointmanager.securityprotocol = securityprotocoltype.ssl3;
            httpwebrequest request = null;
            httpwebresponse response = null;
            request = (httpwebrequest)webrequest.create(posturl);
            request.cookiecontainer = cookiecontainer;
            request.method = header.method;
            request.referer = header.referer;
            request.contenttype = header.contenttype;
            byte[] postdatabyte = encoding.utf8.getbytes(postdata);
            request.contentlength = postdatabyte.length;
            request.allowautoredirect = false;
            request.keepalive = true;
            //提交请求
            stream stream;
            stream = request.getrequeststream();
            stream.write(postdatabyte, 0, postdatabyte.length);
            stream.close();
            //接收响应
            response = (httpwebresponse)request.getresponse();
            using (streamreader reader = new streamreader(response.getresponsestream()))
            {
                restr = reader.readtoend().tostring();
            }
            return restr;
        }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网