c#窗体应用程序通过HTTP协议访问服务器,来验证用户名和密码

用C#写的一个登录程序,我用php写的一个网站,想通过http协议来完成登录客户端到网页的一个用户名和密码的验证,我是菜鸟,求大神们写下详细代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Http;
using System.IO;
using System.Net.NetworkInformation;
using System.Threading;
namespace FinAutoLogin
{
class Program
{
static void Main(string[] args)
{
var ok = ShowNetworkInterfaces();
if (ok)
{
Console.WriteLine("本地网络设备检查完成!");
}
else
{
Console.WriteLine("本地网络未就绪,请检查网卡或网线是否正常!");
Console.Read();
return;
}
Console.WriteLine();
System.Net.ServicePointManager.Expect100Continue = false;
Console.WriteLine("正在请求网关验证服务...");
CookieContainer cc = new CookieContainer();
string postData = "username=XXXX&userpwd=XXXX&login=参数值其他";
byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化
try
{

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://172.16.10.252/cgi-bin/ace_web_auth.cgi"));
request.CookieContainer = cc;
request.Method = "POST";
request.Referer = "http://172.16.100.252/login.php";
request.Accept = "text/html, application/xhtml+xml, */*";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko";
request.AllowAutoRedirect = true;
request.KeepAlive = true;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream newStream = request.GetRequestStream();
// Send the data.
newStream.Write(byteArray, 0, byteArray.Length); //写入参数
newStream.Close();
Console.WriteLine("正在解析网关响应...");
HttpWebResponse response2 = (HttpWebResponse)request.GetResponse();
StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
string text2 = sr2.ReadToEnd();
if (text2.IndexOf("window.location") > -1)
{
Console.WriteLine("网关注册成功!");
}
else
{
Console.WriteLine(text2);
Console.ReadKey();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("按任意键关闭窗口!");
Console.ReadKey();
}
Console.WriteLine("三秒后系统自动退回!");
Thread.Sleep(3000);
}
public static bool ShowNetworkInterfaces()
{
bool isFoundNetWork = false;
Console.WriteLine("正在检查本地网络信息...");
IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces().Where(p=>p.NetworkInterfaceType== NetworkInterfaceType.Ethernet || p.NetworkInterfaceType== NetworkInterfaceType.Wireless80211).ToArray();
Console.WriteLine("{0}.{1} 的网络接口信息: ",computerProperties.HostName, computerProperties.DomainName);
if (nics == null || nics.Length < 1)
{
Console.WriteLine(" 未发现网络接口设备.");
return false;
}
Console.WriteLine(" 发现网络接口设备.................... : {0} 个", nics.Length);
foreach (NetworkInterface adapter in nics)
{
IPInterfaceProperties properties = adapter.GetIPProperties();
Console.WriteLine();
Console.WriteLine(adapter.Name+"---"+adapter.Description);
Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length, '='));
Console.WriteLine(" 接口类型 .......................... : {0}", adapter.NetworkInterfaceType);
Console.WriteLine(" 物理地址 .......................... : {0}", adapter.GetPhysicalAddress().ToString());
Console.WriteLine(" 设备状态 .......................... : {0}", adapter.OperationalStatus);
if(adapter.OperationalStatus== OperationalStatus.Up)
{
isFoundNetWork = true;
}
string versions = "";

// Create a display string for the supported IP versions.
if (adapter.Supports(NetworkInterfaceComponent.IPv4))
{
versions = "IPv4";
}
if (adapter.Supports(NetworkInterfaceComponent.IPv6))
{
if (versions.Length > 0)
{
versions += " ";
}
versions += "IPv6";
}
Console.WriteLine(" IP version .......................... : {0}", versions);
// ShowIPAddresses(properties);
// The following information is not useful for loopback adapters.
// if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback || adapter.NetworkInterfaceType== NetworkInterfaceType.Tunnel)
// {
// continue;
// }
Console.WriteLine(" DNS suffix .............................. : {0}",
properties.DnsSuffix);
string label;
if (adapter.Supports(NetworkInterfaceComponent.IPv4))
{
IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties();
Console.WriteLine(" MTU.................................. : {0}", ipv4.Mtu);
if (ipv4.UsesWins)
{
IPAddressCollection winsServers = properties.WinsServersAddresses;
if (winsServers.Count > 0)
{
label = " WINS Servers ......................... :";
// ShowIPAddresses(label, winsServers);
}
}
}
Console.WriteLine(" DNS enabled .......................... : {0}",
properties.IsDnsEnabled);
Console.WriteLine(" Dynamically configured DNS ........... : {0}",
properties.IsDynamicDnsEnabled);
Console.WriteLine(" Receive Only ......................... : {0}",
adapter.IsReceiveOnly);
Console.WriteLine(" Multicast ............................ : {0}",
adapter.SupportsMulticast);
// ShowInterfaceStatistics(adapter);

}
return isFoundNetWork;
}
}
}追问

比如这个登陆框,我想传递用户名和密码到服务器(php文件)中,然后服务器判断用户名和密码是否正确,返回一个参数。想知道这个代码,不用写那么复杂,能不能帮我简化一点,只写个传递到服务器就好。

追答

System.Net.ServicePointManager.Expect100Continue = false;

到

Console.WriteLine("网关注册成功!"); 之间的代码是必需的。

回复不了,说字数限制,只能上面简单点

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答