c# winform向web传递参数,并获取返回值
展开全部
给你看下我以前做的一个登录器吧。
HttpClient client;
using (client = new HttpClient())
{
client = this.headerSet(client);
string data = " login_username=" + HttpUtility.UrlEncode(username, Encoding.GetEncoding("UTF-8")) + "&login_loginpass=" + pwd + "&login_save=0&aaa=%E7%99%BB%E5%BD%95&login=yes&can_input=0&u=http%3A%2F%2Fm.baidu.com%2F%3Faction%3Dlogin&tpl=&tn=&pu=sz%2540224_220&ssid=0&from=0&bd_page_type=1&uid=wiaui_1318676094_4257";
string aa = client.UploadString("http://wappass.baidu.com/passport/", data);
if (new Regex("登录成功").Match(aa).Success)
{
txtmsgadd(DateTime.Now.ToString("[HH:mm:ss]") + username + "登录成功!", true);
dic.Add(username, pwd);
}
}
返回值什么的只能获取整个页面的前台代码再判断了。用HttpWebRequest和HttpWebResponse
获取整个html页面的前台代码也行的。
/// <summary>
/// 数据截取方法
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string getHtml1(string url)
{
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
StreamReader streamReader = null;
string content = string.Empty;
try
{
webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "GET";
webResponse = (HttpWebResponse)webRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
streamReader = new StreamReader(stream, System.Text.Encoding.GetEncoding("gb2312"));
content = streamReader.ReadToEnd();
}
catch
{
throw;
}
finally
{
streamReader.Close();
webResponse.Close();
}
return content;
}
HttpClient client;
using (client = new HttpClient())
{
client = this.headerSet(client);
string data = " login_username=" + HttpUtility.UrlEncode(username, Encoding.GetEncoding("UTF-8")) + "&login_loginpass=" + pwd + "&login_save=0&aaa=%E7%99%BB%E5%BD%95&login=yes&can_input=0&u=http%3A%2F%2Fm.baidu.com%2F%3Faction%3Dlogin&tpl=&tn=&pu=sz%2540224_220&ssid=0&from=0&bd_page_type=1&uid=wiaui_1318676094_4257";
string aa = client.UploadString("http://wappass.baidu.com/passport/", data);
if (new Regex("登录成功").Match(aa).Success)
{
txtmsgadd(DateTime.Now.ToString("[HH:mm:ss]") + username + "登录成功!", true);
dic.Add(username, pwd);
}
}
返回值什么的只能获取整个页面的前台代码再判断了。用HttpWebRequest和HttpWebResponse
获取整个html页面的前台代码也行的。
/// <summary>
/// 数据截取方法
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string getHtml1(string url)
{
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
StreamReader streamReader = null;
string content = string.Empty;
try
{
webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "GET";
webResponse = (HttpWebResponse)webRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
streamReader = new StreamReader(stream, System.Text.Encoding.GetEncoding("gb2312"));
content = streamReader.ReadToEnd();
}
catch
{
throw;
}
finally
{
streamReader.Close();
webResponse.Close();
}
return content;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询