• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
杰瑞爱好者
博客园    首页    新随笔    联系   管理    订阅  订阅
C#.net模拟提交表单POST(转载)
C#.net模拟提交表单POST

方法一、
System.Net.WebClient WebClientObj = new System.Net.WebClient();
System.Collections.Specialized.NameValueCollection PostVars = new System.Collections.Specialized.NameValueCollection();
PostVars.Add("A1","0");//键值对
PostVars.Add("A2","0");
PostVars.Add("A3","000");

try
{
byte[] byRemoteInfo = WebClientObj.UploadValues("http://www.lovezhao.com/vote.asp","POST",PostVars);
//下面都没用啦,就上面一句话就可以了
string sRemoteInfo = System.Text.Encoding.Default.GetString(byRemoteInfo); //这里的编码方式可能不一样
//这是获取返回信息
    Console.WriteLine(sRemoteInfo );
}
catch
{}



方法二、

CSDN上面的

public static void Test ()
{
WebRequest request = WebRequest.Create("http://soso.music.qq.com/fcgi-bin/fcg_song.fcg");
request.Method = "POST";
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.GetEncoding("gb2312").GetBytes(postData);
//Encoding.UTF8.GetBytes (postData);//你想要的编码方式
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream ();
dataStream.Write (byteArray, 0, byteArray.Length);
dataStream.Close ();
WebResponse response = request.GetResponse ();
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream ();
StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("gb2312"));//这里也要编码.根据你的需要
string responseFromServer = reader.ReadToEnd ();
Console.WriteLine (responseFromServer);
reader.Close ();
dataStream.Close ();
response.Close ();
}

 

可能得到的会乱码,你要自己去看看原来那个网站上的使用的是上面编码

不太好直接 转载,我就只要 把原文 复制下来了.出处见 http://www.blogjava.net/wangxinsh55/archive/2007/06/22/125847.html

/******************************* ****创建人:紫樱桑 ****备注:;欢迎转载,以及提出宝贵的意见 ****本人才疏学浅,愿共同探讨 ********************************/
posted on 2012-12-28 18:24  杰瑞爱好者  阅读(319)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3