c#winform使用post方法定点传送用户名和密码到指定url网页中的 20
c#winform使用post方法定点传送用户名和密码到指定url网页中的账号:;密码:框中,比如说username和password,求稍微具体一点的代码,谢谢...
c#winform使用post方法定点传送用户名和密码到指定url网页中的账号:;密码:框中,比如说username和password,求稍微具体一点的代码,谢谢
展开
展开全部
/// <summary>
/// 创建POST方式的HTTP请求
/// </summary>
/// <param name="url">请求的URL</param>
/// <param name="parameters">随同请求POST的参数名称及参数值字典</param>
/// <param name="timeout">请求的超时时间</param>
/// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
/// <param name="requestEncoding">发送HTTP请求时所用的编码</param>
/// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
/// <returns></returns>
public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int? timeout, string userAgent, Encoding requestEncoding, CookieContainer cookies)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
if (requestEncoding == null)
{
throw new ArgumentNullException("requestEncoding");
}
HttpWebRequest request = null;
//如果是发送HTTPS请求
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
}
else
{
request = WebRequest.Create(url) as HttpWebRequest;
}
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
if (!string.IsNullOrEmpty(userAgent))
{
request.UserAgent = userAgent;
}
else
{
request.UserAgent = DefaultUserAgent;
}
if (timeout.HasValue)
{
request.Timeout = timeout.Value;
}
if (cookies != null)
{
request.CookieContainer = cookies;
//request.CookieContainer.Add(cookies);
}
//如果需要POST数据
if (!(parameters == null || parameters.Count == 0))
{
StringBuilder buffer = new StringBuilder();
int i = 0;
foreach (string key in parameters.Keys)
{
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
}
i++;
}
byte[] data = requestEncoding.GetBytes(buffer.ToString());
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
return request.GetResponse() as HttpWebResponse;
}
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受
}
}
/// 创建POST方式的HTTP请求
/// </summary>
/// <param name="url">请求的URL</param>
/// <param name="parameters">随同请求POST的参数名称及参数值字典</param>
/// <param name="timeout">请求的超时时间</param>
/// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
/// <param name="requestEncoding">发送HTTP请求时所用的编码</param>
/// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
/// <returns></returns>
public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int? timeout, string userAgent, Encoding requestEncoding, CookieContainer cookies)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
if (requestEncoding == null)
{
throw new ArgumentNullException("requestEncoding");
}
HttpWebRequest request = null;
//如果是发送HTTPS请求
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
}
else
{
request = WebRequest.Create(url) as HttpWebRequest;
}
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
if (!string.IsNullOrEmpty(userAgent))
{
request.UserAgent = userAgent;
}
else
{
request.UserAgent = DefaultUserAgent;
}
if (timeout.HasValue)
{
request.Timeout = timeout.Value;
}
if (cookies != null)
{
request.CookieContainer = cookies;
//request.CookieContainer.Add(cookies);
}
//如果需要POST数据
if (!(parameters == null || parameters.Count == 0))
{
StringBuilder buffer = new StringBuilder();
int i = 0;
foreach (string key in parameters.Keys)
{
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
}
i++;
}
byte[] data = requestEncoding.GetBytes(buffer.ToString());
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
return request.GetResponse() as HttpWebResponse;
}
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受
}
}
展开全部
首先创建一个windows应用程序和web应用程序。
在web应用程序中,将网页切换到源代码并把源代码中一些没用的代码删掉,只保留头部,在windows应用程序读取网页源码时,这些都会被一起读下来,但这些都是没用的数据,而且删掉没什么影响。需要保留的代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
然后在Page_Load中实现功能,以下为一个将两个数相加并将和返回给winform的网页例子。
string A = Request.Form["a"].ToString();//由winform中传给网页的参数用a标识
string B = Request.Form["b"].ToString();
int C = Convert.ToInt32(A) + Convert.ToInt32(B);
Response.Write(C.ToString());//将数据写到网页中
在winform中实现传参的例子代码如下:
private void button1_Click(object sender, EventArgs e)
{
WebClient w = new WebClient();
System.Collections.Specialized.NameValueCollection VarPost = new System.Collections.Specialized.NameValueCollection();
VarPost.Add("a", textBox1.Text.Trim());//将textBox1中的数据变为用a标识的参数,并用POST传值方式传给网页
VarPost.Add("b", textBox2.Text.Trim());
//将参数列表VarPost中的所有数据用POST传值的方式传给http://申请好的域名或用IIs配置好的地址/Default.aspx,
//并将从网页上返回的数据以字节流存放到byRemoteInfo中)(注:IIS配置的时候经常没配置好会提示错误,嘿)
byte[] byRemoteInfo = w.UploadValues("http://申请好的域名或用IIs配置好的地址/Default.aspx", "POST", VarPost);
string sRemoteInfo = System.Text.Encoding.UTF8.GetString(byRemoteInfo);
label1.Text = sRemoteInfo;
}
这样就实现了C# Winform利用POST传值方式模拟表单提交数据,在这仅供大家参考。
在web应用程序中,将网页切换到源代码并把源代码中一些没用的代码删掉,只保留头部,在windows应用程序读取网页源码时,这些都会被一起读下来,但这些都是没用的数据,而且删掉没什么影响。需要保留的代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
然后在Page_Load中实现功能,以下为一个将两个数相加并将和返回给winform的网页例子。
string A = Request.Form["a"].ToString();//由winform中传给网页的参数用a标识
string B = Request.Form["b"].ToString();
int C = Convert.ToInt32(A) + Convert.ToInt32(B);
Response.Write(C.ToString());//将数据写到网页中
在winform中实现传参的例子代码如下:
private void button1_Click(object sender, EventArgs e)
{
WebClient w = new WebClient();
System.Collections.Specialized.NameValueCollection VarPost = new System.Collections.Specialized.NameValueCollection();
VarPost.Add("a", textBox1.Text.Trim());//将textBox1中的数据变为用a标识的参数,并用POST传值方式传给网页
VarPost.Add("b", textBox2.Text.Trim());
//将参数列表VarPost中的所有数据用POST传值的方式传给http://申请好的域名或用IIs配置好的地址/Default.aspx,
//并将从网页上返回的数据以字节流存放到byRemoteInfo中)(注:IIS配置的时候经常没配置好会提示错误,嘿)
byte[] byRemoteInfo = w.UploadValues("http://申请好的域名或用IIs配置好的地址/Default.aspx", "POST", VarPost);
string sRemoteInfo = System.Text.Encoding.UTF8.GetString(byRemoteInfo);
label1.Text = sRemoteInfo;
}
这样就实现了C# Winform利用POST传值方式模拟表单提交数据,在这仅供大家参考。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询