解决方案 »
- 怎么实现根据应用程序加载时间的进度更新进度条
- c#下调用VC++链接库
- 请教一个正则表达式,有关帖子都看过了,找不到答案,请达人帮忙,100分
- 求助啊,各位大神,,,,,,,,,,,,,,标题一定要长啊
- 知道了怎么改SharpZipLib使其支持中文, 如何将其重编译?
- 站内短消息例子
- winform中dataGrid记录删除问题?
- 向C#高手请教SqlParameter构造函数重载函数问题?望不吝赐教
- 如何点击一个按钮打开一个窗口的同时将现有窗体关闭
- C#使用dubbo并注册到zookeeper
- VS2010Express 为什么每次新建项目都会出现个警告提示
- WebFrom程序(C#)使用下面3个控件来实现读取excel
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;namespace ConsoleApplication1
{
class Program
{
private static HttpListener listener; static void Main(string[] args)
{
if (listener == null)
{
listener = new HttpListener();
var url = "http://+:9876/";
listener.Prefixes.Add(url);
listener.Start();
listener.BeginGetContext(MainProcess, null);
dic.Add("abcd", test3);
dic.Add("ef", test2);
dic.Add("12", test1);
Console.ReadKey();
};
} private static void MainProcess(IAsyncResult ar)
{
var context = listener.EndGetContext(ar);
listener.BeginGetContext(MainProcess, null);
var response = context.Response;
response.AddHeader("Server", "My Server V0.0.1");
var request = context.Request;
var path = request.Url.LocalPath;
if (path.StartsWith("/") || path.StartsWith("\\"))
path = path.Substring(1);
var visit = path.Split(new char[] { '/', '\\' }, 2);
if (visit.Length > 0)
{
var cmd = visit[0].ToLower();
Action<HttpListenerContext, string[]> actor;
if (dic.TryGetValue(cmd, out actor))
actor(context, visit);
}
} private static Dictionary<string, Action<HttpListenerContext, string[]>> dic = new Dictionary<string, Action<HttpListenerContext, string[]>>(); private static void test1(HttpListenerContext context, string[] path)
{
var hd = context.Request.Headers;
var result = from x in hd.AllKeys
select "<p><span class='key'>" + x + "</span><span class='val'>" + hd[x] + "</span></p>";
using (var wt = new StreamWriter(context.Response.OutputStream))
wt.Write(string.Join("", result));
} private static void test2(HttpListenerContext context, string[] path)
{
context.Response.StatusCode = (int)HttpStatusCode.NotImplemented;
context.Response.Close();
} private static void test3(HttpListenerContext context, string[] path)
{
var sb = new StringBuilder("输入请求:");
sb.AppendLine(context.Request.Url.LocalPath);
sb.AppendLine(string.Format("执行命令:{0}", path[0]));
sb.AppendLine(string.Format("另外有{0}个参数", path.Length - 1 + context.Request.QueryString.Count));
sb.AppendLine(DateTime.Now.ToString());
context.Response.ContentType = "text/plain;charset=UTF-8";
var result = Encoding.UTF8.GetBytes(sb.ToString());
using (var stream = context.Response.OutputStream)
{
stream.Write(result, 0, result.Length);
}
}
}
}
http://localhost:9876/abcd/aa.jsp?p=oooo&d=kslasdfdfs
http://localhost:9876/ef/
http://localhost:9876/12/
public bool RequestLoginInput()
{
bool retval = true; string sServerUrl = @"http://XX.XX.XX.XX/login.jsp";
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sServerUrl); req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = 0;
string ResponseStr;
req.AllowAutoRedirect = false;
req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
req.KeepAlive = true;
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)";
req.ContentType = "application/x-www-form-urlencoded";
try
{
using (HttpWebResponse wr = (HttpWebResponse)req.GetResponse())
{
Stream ResStream = wr.GetResponseStream();
string tempString = null;
int count = 0;
byte[] buf = new byte[8192];
StringBuilder sb = new StringBuilder(); do
{
// fill the buffer with data
count = ResStream.Read(buf, 0, buf.Length); // make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.UTF8.GetString(buf, 0, count); // continue building the string
sb.Append(tempString);
}
} while (count > 0); // any more data to read?
ResponseStr = sb.ToString();
} object[] oPageText = { ResponseStr };
HTMLDocument document = new HTMLDocumentClass();
IHTMLDocument2 doc = (IHTMLDocument2)document;
try
{
string strResponse = ResponseStr;
doc.write(oPageText);
if (null != document)
{
}
}
catch
{
} mshtml.HTMLDocumentClass logindocument = (mshtml.HTMLDocumentClass)document;
mshtml.HTMLFormElement loginform = (mshtml.HTMLFormElement)logindocument.getElementById("loginform");
mshtml.HTMLInputElement username = (mshtml.HTMLInputElement)logindocument.getElementById("username");
mshtml.HTMLInputElement textPassword = (mshtml.HTMLInputElement)logindocument.getElementById("password");
mshtml.HTMLInputElement rememberpwd = (mshtml.HTMLInputElement)logindocument.getElementById("savepwd"); mshtml.HTMLInputElement localArea = (mshtml.HTMLInputElement)logindocument.getElementById("localArea");
mshtml.HTMLInputElement localNeedAuthId = (mshtml.HTMLInputElement)logindocument.getElementById("localNeedAuthId");
mshtml.HTMLInputElement roamNeedAuthID = (mshtml.HTMLInputElement)logindocument.getElementById("roamNeedAuthID");
mshtml.HTMLInputElement allowNumber = (mshtml.HTMLInputElement)logindocument.getElementById("allowNumber");
mshtml.HTMLSelectElement userOpenAddress = (mshtml.HTMLSelectElement)logindocument.getElementById("userOpenAddress");
mshtml.HTMLInputElement passwordType = (mshtml.HTMLInputElement)logindocument.getElementById("passwordType"); LocalArea = localArea.value;
LocalNeedAuthId = localNeedAuthId.value;
RoamNeedAuthID = roamNeedAuthID.value;
AllowNumber = allowNumber.value;
Username = Properties.Settings.Default.username;
UserOpenAddress = userOpenAddress.value;
Password = Properties.Settings.Default.password;
PasswordType = passwordType.value; }
catch
{
retval = false;
}
finally
{
}
return retval;
}