我现在再用一个开发包(一些脚本语言)。这个开发包里面只提供了 http 通信的例子,没有 socket 的例子。【背景完毕】我想用C#和这个开发包里面的脚本进行通信,但是不太会写C#这边的服务器的内容,我就是想有个类,能够不接收脚本那边发来的请求,然后进行回馈。以前有点 JAVA 的基础,JAVA里面要通过阿帕奇才能够完成一个服务器。我想问一下,C#能不能简单一点,最好有个例子,就是一个类,简单设置url,然后让脚本向url发送请求,这边的C#回应,谢谢了。
调试欢乐多
http://dotnet.aspx.cc/file/HttpWebRequest-Download-Http-Url.aspx
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访问。
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;
}
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();
}
当然创建asp.net也好,还是自己写http服务器也好。
都可以达到目的。我的出发点是想说用WCF做的http服务是最快的捷径。
另外,服务端的代码也看不到解析QueryString,
使用Response什么的,更加面向对象。
WCF是个好东西.
顶一下