zoukankan      html  css  js  c++  java
  • POST GET原理函数

      一、C#客户端通过POST或GET向指定的网址发送数据

      发送请求

      /// <summary>

      /// 发送请求

      /// </summary>

      /// <param name="url">网址</param>

      /// <param name="parameter">要发送的值。如:abc=4&bcd=5</param>

      /// <param name="method">发送的方式,“POST”还是“GET”</param>

      /// <returns>返回的结果</returns>

      public static string RequestUrl(string url, string parameter, string method)

      {

        try

        {

          HttpWebRequest hwrq = null;

          if (method == "POST")

          {

            hwrq = (HttpWebRequest)HttpWebRequest.Create(url);

            hwrq.KeepAlive = false;

            hwrq.ReadWriteTimeout = 10000;

            //hwrq.CookieContainer = cc;

            hwrq.Method = method;

            byte[] postData = System.Text.Encoding.UTF8.GetBytes(parameter);

            hwrq.ContentType = "application/x-www-form-urlencoded";

            hwrq.ContentLength = postData.Length;

            Stream writeStream = hwrq.GetRequestStream();

            writeStream.Write(postData, 0, postData.Length);

            writeStream.Close();

          }

          else if (method == "GET")

          {

            hwrq = (HttpWebRequest)HttpWebRequest.Create(url + "?" + System.Web.HttpUtility.UrlEncode(parameter));

            hwrq.KeepAlive = false;

            //hwrq.CookieContainer = cc;

            hwrq.Method = method;

          }

          if (hwrq != null)

          {

            HttpWebResponse hwrp = (HttpWebResponse)hwrq.GetResponse();

            //return hwrp.ResponseUri.AbsoluteUri;

            StreamReader sr = new StreamReader(hwrp.GetResponseStream(), Encoding.Default);

            return sr.ReadToEnd();

          }

        }

        catch (Exception ex)

        {

          throw ex;

        }

        return null;

       }

     二、邮件发送函数

      邮件发送

      /// <summary>

      /// 发送邮件

      /// </summary>

      /// <param name="strSmtpServer">Smtp地址</param>

      /// <param name="strFrom">发送方的邮件地址</param>

      /// <param name="strFromPass">发送方的邮件密码</param>

      /// <param name="strto">接受方的邮件地址</param>

      /// <param name="strSubject">邮件主题</param>

      /// <param name="strBody">邮件内容,支持html</param>

      /// <param name="Attachments">附件列表</param>

      /// <returns>成功与否</returns>

      public static bool SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody, string[] Attachments)

      {

        System.Net.Mail.SmtpClient client = null;

        System.Net.Mail.MailMessage message = null;

        try

        {

          client = new SmtpClient();

          client.Host = System.Net.Dns.GetHostAddresses(strSmtpServer)[0].ToString();

          client.UseDefaultCredentials = false;

          client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);

          //星号改成自己邮箱的密码

          client.DeliveryMethod = SmtpDeliveryMethod.Network;

          message = new MailMessage(strFrom, strto);

          message.Subject = strSubject;

          message.Body = strBody;

          message.BodyEncoding = System.Text.Encoding.UTF8;

          message.IsBodyHtml = true;

          //添加附件

          foreach (string forStr in Attachments)

          {

            Attachment data = new Attachment(forStr, System.Net.Mime.MediaTypeNames.Application.Octet);

            message.Attachments.Add(data);

          }

          client.Send(message);

        }

        catch (Exception ex)

        {

          using (System.IO.StreamWriter sw = new System.IO.StreamWriter(Directory.GetCurrentDirectory().TrimEnd('\\') +         "http://www.cnblogs.com/JackieYang/admin/file://Log.txt/", true, Encoding.UTF8))

          {

            sw.Write("发送邮件出错!\n" + ex.Message + "\n" + ex.StackTrace + "\n===========================\n");

          }

          return false;

        }

        finally

        {

          if (message != null)

          {

            foreach (Attachment forData in message.Attachments)

            forData.Dispose();

            message.Attachments.Clear();

            message.Dispose();

          }

        }

        return true;

      }

      三:HttpWebRequest 对象和 HttpWebResponse 对象介绍:

    在.NET的基本类型库类中提供了两个对象类:HTTPWebRequest和HTTPWebResponse,分别用来向某资源发送请求和获得响应。为了得到一个资源的内容,我们先指定一个想要抓取的URL地址,用C# HTTPWebRequest对象进行请求,用HTTPWebResponse对象接收响应的结果,最后用TextStream对象来提取我们想要的信息,并在控制台打印出来。

     

     

    using System.IO;

    using System.Net;

    using System.Text;

    private void button1_Click(object sender, System.EventArgs e)

    {   

      byte[] buf = new byte[38192];

      HttpWebRequest request = (HttpWebRequest)

      WebRequest.Create(textBox1.Text);

      HttpWebResponse response = (HttpWebResponse)

      request.GetResponse();

      Stream resStream = response.GetResponseStream();

      int count = resStream.Read(buf, 0, buf.Length);

      textBox2.Text = Encoding.Default.GetString(buf, 0,

      count);

      resStream.Close();

    }

    上面的这个程序的功能是抓取网页http://lucky.myrice.com/down.htm的内容,并在多行文本框里显示出HTML代码,由于返回的数据是字节类型的,因此,我们创建一个名为buf的字节类型的数组变量来存储请求返回来的结果,其中数组的大小与我们要请求返回的数据大小有关系。首先,我们实例化C# HTTPWebRequest对象,使用WebRequest类的静态方法Create(),该方法的字符串参数就是我们要请求页面的URL地址,由于Create()方法返回的是WebRequest类型的,我们必须对它进行造型(即类型转换)类型,成HttpWebRequest再赋给request变量。一旦我们建立了C# HTTPWebRequest对象,就可以使用它的GetResponse()方法来返回一个WebResponse对象,然后再造型成HttpWebResponse对象赋给response变量。现在,就可以使用response对象的GetResponseStream()方法来得到响应的文本流了,最后用Stream对象的Read()方法把返回的响应信息放到我们最初创建的字节数组buf中,Read()有3个参数,分别是:要放入的字节数组,字节数组的开始位置,字节数组的长度。最后把字节转换成字符串,注意:这里采用的采用的是Default编码,它使用默认的编码方式,我们就不用再进行字符编码之间的转换了。

  • 相关阅读:
    嵌入级联分类器
    AdaBoost 和 Real Adaboost 总结
    二分图匹配--匈牙利算法
    更新说明
    使用Visual Studio 2015 Community 开发windows服务
    C#字符串的不变性
    Windows 7 IIS HTTP 错误 500.21 – Internal Server Error 解决方法
    asp.net的请求管道事件
    Http请求过程
    css简单学习属性2---背景图片
  • 原文地址:https://www.cnblogs.com/JackieYang/p/1670114.html
Copyright © 2011-2022 走看看