zoukankan      html  css  js  c++  java
  • ashx获取另一个页面的返回内容

    ashx获取另一个页面的值:
    注:因为这是web请求,所以要http://...web路径
    using System;
    using System.Web;
    using System.Web.SessionState;
    using System.IO;
    using System.Net;
    using System.Text;
    
    
    
    public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
           
            context.Response.Write(GetHtml("http://......", ""));
            context.Response.End();
        }
    
        //post提交
        public static string GetHtml(string posturl, string postData)
        {
            Stream outstream = null;
            Stream instream = null;
            StreamReader sr = null;
            HttpWebResponse response = null;
            HttpWebRequest request = null;
            Encoding encoding = System.Text.Encoding.GetEncoding("UTF-8");
            byte[] data = encoding.GetBytes(postData);
            // 准备请求...
            try
            {
                // 设置参数
                request = WebRequest.Create(posturl) as HttpWebRequest;
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                outstream = request.GetRequestStream();
                outstream.Write(data, 0, data.Length);
                outstream.Close();
                //发送请求并获取相应回应数据
                response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求
                instream = response.GetResponseStream();
                sr = new StreamReader(instream, encoding);
                //返回结果网页(html)代码
                string content = sr.ReadToEnd();
                string err = string.Empty;
                return content;
            }
            catch (Exception ex)
            {
                string err = ex.Message;
                return string.Empty;
            }
        }
  • 相关阅读:
    js将时间戳转化为时间,(不省略0)
    SpringMVC路径传参
    贪心算法及实例
    剪绳子(贪心算法)
    c语言输入一行未知个数数字存入数组
    Netlink机制详解
    new和malloc的区别
    堆 和 栈的 区别
    strlen与sizeof区别(转载)
    任务
  • 原文地址:https://www.cnblogs.com/zhubenxi/p/5166013.html
Copyright © 2011-2022 走看看