Form1.cs
上传用户:jsz11269
上传日期:2017-01-14
资源大小:450k
文件大小:2k
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.IO;
- namespace WebRequestAndResponse
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void btnGet_Click(object sender, EventArgs e)
- {
- WebRequest req = WebRequest.Create(tbURL.Text);
- WebResponse res = req.GetResponse();
- Stream ReceiveStream = res.GetResponseStream();
- Encoding ecode = Encoding.GetEncoding("utf-8");
- StreamReader sr = new StreamReader(ReceiveStream, ecode);
- Char[] ReadBuffer = new char[256];
- int nCount = sr.Read(ReadBuffer, 0, 256);
- while (nCount > 0)
- {
- string str = new string(ReadBuffer, 0, nCount);
- tbContent.Text += str;
- nCount = sr.Read(ReadBuffer, 0, 256);
- }
- }
- private void btnGet2_Click(object sender, EventArgs e)
- {
- WebRequest req = WebRequest.Create(tbURL.Text);
- req.Method = "POST";
- req.ContentType = "application/x-www.form-urlencoded";
- byte[] bData = null;
- bData = Encoding.UTF8.GetBytes(tbPostData.Text);
- req.ContentLength = bData.Length;
- Stream newStream = req.GetRequestStream();
- newStream.Write(bData, 0, bData.Length);
- newStream.Close();
- WebResponse res = req.GetResponse();
- Stream ReceiveStream = res.GetResponseStream();
- Encoding ecode = Encoding.GetEncoding("utf-8");
- StreamReader sr = new StreamReader(ReceiveStream, ecode);
- Char[] ReadBuffer = new char[256];
- int nCount = sr.Read(ReadBuffer, 0, 256);
- while (nCount > 0)
- {
- string str = new string(ReadBuffer, 0, nCount);
- tbContent.Text += str;
- nCount = sr.Read(ReadBuffer, 0, 256);
- }
- }
- }
- }