我现在再用一个开发包(一些脚本语言)。这个开发包里面只提供了 http 通信的例子,没有 socket 的例子。【背景完毕】我想用C#和这个开发包里面的脚本进行通信,但是不太会写C#这边的服务器的内容,我就是想有个类,能够不接收脚本那边发来的请求,然后进行回馈。以前有点 JAVA 的基础,JAVA里面要通过阿帕奇才能够完成一个服务器。我想问一下,C#能不能简单一点,最好有个例子,就是一个类,简单设置url,然后让脚本向url发送请求,这边的C#回应,谢谢了。

解决方案 »

  1.   

    例子
    http://dotnet.aspx.cc/file/HttpWebRequest-Download-Http-Url.aspx
      

  2.   

    两种方式开发你的服务器:1. 自己开发一个服务器。使用windows服务、控制台程序、窗体程序等都可以。参考:
    http://www.google.com.hk/search?q=httplistener&hl=zh-CN&safe=strict&prmd=ivns&source=lnt&tbs=lr:lang_1zh-CN|lang_1zh-TW&lr=lang_zh-CN|lang_zh-TW&sa=X&ei=jPBlTcSODoi8cN3O2I0M&ved=0CAcQpwUoAQ2. 创建一个asp.net网站,使用ashx来处理客户端http访问。
      

  3.   

    HttpWebRequest通过post等提交到相关页面,获取执行的页面数据
    private string HttpWebRequestLogin(string loginUrl, string postData, ref CookieContainer cookieContainer)   
    {   
      byte[] bdata = Encoding.Default.GetBytes(postData);   
      System.Net.HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(loginUrl);   
      myRequest.Method = "POST";   
      myRequest.ContentType = "application/x-www-form-urlencoded";   
      myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";   
      myRequest.Referer = loginUrl;   
      myRequest.KeepAlive = true;   
      myRequest.AllowAutoRedirect = true;   
      if (cookieContainer == null)   
      cookieContainer = new CookieContainer();   
      myRequest.CookieContainer = cookieContainer;   
      myRequest.ContentLength = bdata.Length;   
      Stream newStream = myRequest.GetRequestStream();   
      newStream.Write(l_data, 0, bdata.Length);   
      newStream.Close();   
      HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();   
      StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("GB2312"));   
      string content = reader.ReadToEnd();   
      return content;   
    }   
     
     
      

  4.   

    Google出来成千上个例子代码,你看了搜索结果了吗?
      

  5.   

    用WCF写一个RESTful的服务。你可以将下面的服务发布在IIS上。WCF ServerClient:using System;
    using System.ServiceModel;
    using System.ServiceModel.Activation;
    using System.ServiceModel.Web;namespace WcfRestService1
    {
        [ServiceContract]
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
        public class Service1
        {
            [WebGet(UriTemplate = "{content}")]
            public string Get(string content)
            {
                return "server echo: " + content;
            }
        }
    }
    客户端消费:
    http://localhost:1991/Service1/haha
    这样就会返回:"server echo: haha" 的字符串
    static void Main(string[] args)
    {
        var url = "http://localhost:1991/Service1/";
        var reqUrl = url + "test";
        WebClient client = new WebClient();
        var str = client.DownloadString(reqUrl);
        Console.WriteLine(str);
        Console.Read();
    }
      

  6.   

    我的意思是说,如果有直截了当地、我们一看就懂得词的话就不用“消费”这个词了。比如lz说了“http通信的例子”,这就非常直截了当,我们知道http的最常用访问命令就是get和post。在没有更多堆层次的描述的时候,我猜lz也就是开发一个基本http服务器才能满足要求。
      

  7.   

    好吧,我承认有点拽词了。这里只是个sample,演示下客户端怎么使用WCF的Rest服务。
    当然创建asp.net也好,还是自己写http服务器也好。
    都可以达到目的。我的出发点是想说用WCF做的http服务是最快的捷径。
    另外,服务端的代码也看不到解析QueryString,
    使用Response什么的,更加面向对象。
      

  8.   


    WCF是个好东西.
    顶一下