Form1.cs
上传用户:jsz11269
上传日期:2017-01-14
资源大小:450k
文件大小:2k
源码类别:

Email服务器

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Net;
  9. using System.IO;
  10. namespace WebRequestAndResponse
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.         private void btnGet_Click(object sender, EventArgs e)
  19.         {
  20.             WebRequest req = WebRequest.Create(tbURL.Text);
  21.             WebResponse res = req.GetResponse();
  22.             Stream ReceiveStream = res.GetResponseStream();
  23.             Encoding ecode = Encoding.GetEncoding("utf-8");
  24.             StreamReader sr = new StreamReader(ReceiveStream, ecode);
  25.             Char[] ReadBuffer = new char[256];
  26.             int nCount = sr.Read(ReadBuffer, 0, 256);
  27.             while (nCount > 0)
  28.             {
  29.                 string str = new string(ReadBuffer, 0, nCount);
  30.                 tbContent.Text += str;
  31.                 nCount = sr.Read(ReadBuffer, 0, 256);
  32.             }
  33.         }
  34.         private void btnGet2_Click(object sender, EventArgs e)
  35.         {
  36.             WebRequest req = WebRequest.Create(tbURL.Text);
  37.             req.Method = "POST";
  38.             req.ContentType = "application/x-www.form-urlencoded";
  39.             byte[] bData = null;
  40.             bData = Encoding.UTF8.GetBytes(tbPostData.Text);
  41.             req.ContentLength = bData.Length;
  42.             Stream newStream = req.GetRequestStream();
  43.             newStream.Write(bData, 0, bData.Length);
  44.             newStream.Close();
  45.             WebResponse res = req.GetResponse();
  46.             Stream ReceiveStream = res.GetResponseStream();
  47.             Encoding ecode = Encoding.GetEncoding("utf-8");
  48.             StreamReader sr = new StreamReader(ReceiveStream, ecode);
  49.             Char[] ReadBuffer = new char[256];
  50.             int nCount = sr.Read(ReadBuffer, 0, 256);
  51.             while (nCount > 0)
  52.             {
  53.                 string str = new string(ReadBuffer, 0, nCount);
  54.                 tbContent.Text += str;
  55.                 nCount = sr.Read(ReadBuffer, 0, 256);
  56.             }
  57.         }
  58.     }
  59. }