WebForm
前台
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> <asp:Label ID="Label1" runat="server" Text="Label">返回值</asp:Label>
后台
protected void Button1_Click(object sender, EventArgs e) { //1 //string url = "地址和参数"; //try //{ // HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest; // req.ContentType = "application/x-www-form-urlencoded"; // req.Accept = "*/*"; // req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)"; // req.Timeout = 10000;//10秒连接不成功就中断 // req.Method = "post"; // HttpWebResponse response = req.GetResponse() as HttpWebResponse; // StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default); // this.Label1.Text = sr.ReadToEnd(); //} //catch (WebException ex) //{ // Console.WriteLine(ex.ToString()); //} //Console.WriteLine(url); //2 string postData = "参数"; // 要发放的数据 string postUrl = "地址"; byte[] byteArray = Encoding.Default.GetBytes(postData); //转化 HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); webReq.Method = "POST"; webReq.ContentType = "application/x-www-form-urlencoded"; webReq.ContentLength = byteArray.Length; Stream newStream = webReq.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length);//写入参数 newStream.Close(); HttpWebResponse response = (HttpWebResponse)webReq.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default); this.Label1.Text = sr.ReadToEnd(); sr.Close(); response.Close(); newStream.Close(); //3 //byte[] postData = Encoding.UTF8.GetBytes("参数");//这里即为传递的参数,可以用工具抓包分析,也可以自己分析,主要是form里面每一个name都要加进来 编码 //string url = "地址";//地址 //WebClient webClient = new WebClient(); //webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可 //byte[] responseData = webClient.UploadData(url, "POST", postData);//得到返回字符流 //string srcString = Encoding.UTF8.GetString(responseData);//解码 //this.Label1.Text = srcString; //4 //string postData = "参数"; // 要发放的数据 //string postUrl = "地址"; //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl); //request.Method = "POST"; //request.ContentType = "application/x-www-form-urlencoded"; //request.ContentLength = Encoding.UTF8.GetByteCount(postData); ////request.CookieContainer = cookie;//获取记录(储存在用户本地终端上的数据) //Stream myRequestStream = request.GetRequestStream(); //StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312")); //myStreamWriter.Write(postData); //myStreamWriter.Close(); //HttpWebResponse response = (HttpWebResponse)request.GetResponse(); ////response.Cookies = cookie.GetCookies(response.ResponseUri);//获取记录(储存在用户本地终端上的数据) //Stream myResponseStream = response.GetResponseStream(); //StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); //string retString = myStreamReader.ReadToEnd(); //this.Label1.Text = retString; //myStreamReader.Close(); //myResponseStream.Close(); }
这些都是到处总结出来的