我对网络编程不太熟,所以请教。
场景是:客户端发送一个request,比如http://192.168.0.1/,服务器端接收到这个request并解析,根据内容,返回一个自己定制的response内容。
请给出一个架构代码,谢谢。

解决方案 »

  1.   

    客户端 http://192.168.0.1?id=2
    服务器端 if(Request.QueryString["id"]==2)
              response.write();
      

  2.   

    对不起,我不知道代码在哪里写。能不能给一个稍微完整的代码结构,比如说,是在双击button后面写等等。
      

  3.   

     这是简单的方式,很多默认的请求属性都没有列出
    protected string GetHtml()
        {
            HttpWebRequest wres = WebRequest.Create("http://edu.sina.com.cn/bbs/2007/1017/1411014865.html") as HttpWebRequest;        WebResponse webResponse = wres.GetResponse();
            long l = webResponse.ContentLength;
            Uri u = webResponse.ResponseUri;
            System.IO.StreamReader sr = new System.IO.StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));        string HTML = sr.ReadToEnd();        
            //操作返回的html字符串
            int start, stop;
            string temp = "";
            start = HTML.IndexOf("<div id=sudsclickstreamdiv style='position:absolute;width:80;top:-300;left:100;visibility:hidden;z-index:1'></div>", 0, HTML.Length);
            stop = HTML.IndexOf("<!-- END Nielsen//NetRatings SiteCensus V5.2 -->", start);
            temp = HTML.Substring(start, stop - start);        Response.Write(HTML);
            Response.End();
            return temp;
        }
      

  4.   

    try
            {
                // 要提交表单的URI字符串。
                string uriString = "http://localhost/web/index.aspx";            ///////////////////////////////////////
                // 打开页面
                ///////////////////////////////////////
                WebClient webClient = new WebClient();
                byte[] responseData = webClient.DownloadData(uriString);
                string srcString = Encoding.UTF8.GetString(responseData);            ///////////////////////////////////////
                // 填写页面并提交
                ///////////////////////////////////////
                webClient = new WebClient();
                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");            // 获取页面的 VeiwState                
                string viewStateFlag = "id=\"__VIEWSTATE\" value=\"";
                int i = srcString.IndexOf(viewStateFlag) + viewStateFlag.Length;
                int j = srcString.IndexOf("\"", i);
                string viewState = srcString.Substring(i, j - i);            // 获取页面的 EventValidation                
                string eventValidationFlag = "id=\"__EVENTVALIDATION\" value=\"";
                i = srcString.IndexOf(eventValidationFlag) + eventValidationFlag.Length;
                j = srcString.IndexOf("\"", i);
                string eventValidation = srcString.Substring(i, j - i);            // 提交按钮的文本
                string submitButton = "登录";            viewState = System.Web.HttpUtility.UrlEncode(viewState);
                eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
                submitButton = System.Web.HttpUtility.UrlEncode(submitButton);            // 要提交的字符串数据。格式形如:user=uesr1&password=123
                string postString = "userName=1&password=1" + "&loginButton=" + submitButton + "&__VIEWSTATE=" + viewState + "&__EVENTVALIDATION=" + eventValidation;
                // 将字符串转换成字节数组
                byte[] postData = Encoding.ASCII.GetBytes(postString);
                // 上传数据,返回页面的字节数组
                responseData = webClient.UploadData(uriString, "POST", postData);
                // 将返回的将字节数组转换成字符串(HTML);
                // ASP.NET 返回的页面一般是Unicode,如果是简体中文应使用 
                //   Encoding.GetEncoding("GB2312").GetString(responseData)
                srcString = Encoding.UTF8.GetString(responseData);            ///////////////////////////////////////
                // 分析返回的页面
                ///////////////////////////////////////
                //  
            }
            catch (WebException we)
            {
                string msg = we.Message;
            }
    不知道能不能用得上
      

  5.   

    非常感谢上两位,代码已经很清楚了,我也知道用什么类了。
    但我还有一点疑惑,就是这些代码放在哪个位置?以我的认识,是不是放到某个链接button的单击事件处理函数里,或其他什么地方,也就是服务器端如何调用这些代码?总得有个机制连接客户端的request吧?比如说,单击button。不知道我得理解对不对?
      

  6.   

    Page_Load中
    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    //根据传值prono(Project Number)绑定
    if(!Object.Equals(Request.QueryString["prono"],null)&&Request.QueryString["prono"].ToString()!="")
    {
    this.txtNumber.Text=Request.QueryString["prono"];
    GridBind(1);
    MyGrid1.SelectedIndex=0;
    this.hidID.Value=MyGrid1.DataKeys[MyGrid1.SelectedIndex].ToString();
    }
    else
    {
    GridBind(1);
    MyGrid1.SelectedIndex=0;
    this.hidID.Value=MyGrid1.DataKeys[MyGrid1.SelectedIndex].ToString();
    }
    }