调用一个url发送短信

需要在WinForm里调用一个url发送短信,为了防止自己忘记,特将HttpWebRequest的使用方法摘录如下:


public void SendSms(string phoneNumber, string text)
  {
      string url;
        url = string.Format(@"http://a.b.c.d/path/?user=username&password=password&phonenumber={0}&text={1}&charset=gb2312", phoneNumber, HttpUtility.UrlEncode(text));
       HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
       request.MaximumResponseHeadersLength = 10240;
       request.Credentials = CredentialCache.DefaultCredentials;
       request.Method = "GET";
        request.Timeout = 60000;
       WebResponse webRep = request.GetResponse();
        Debug.WriteLine(webRep.Headers.ToString());
        Debug.WriteLine(webRep.ResponseUri.OriginalString);
       #region 读取网页内容/不需要
        // Obtain a 'Stream' object associated with the response object.
        Stream ReceiveStream = webRep.GetResponseStream();
        Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
        // Pipe the stream to a higher level stream reader with the required encoding format.
        StreamReader readStream = new StreamReader(ReceiveStream, encode);
        Console.WriteLine("\nResponse stream received");
        Char[] read = new Char[256];
        // Read 256 charcters at a time.   
        int count = readStream.Read(read, 0, 256);
        Console.WriteLine("HTML\r\n");
        while (count > 0) {
            // Dump the 256 characters on a string and display the string onto the console.
            String str = new String(read, 0, count);
            Console.Write(str);
            count = readStream.Read(read, 0, 256);
        }
        Console.WriteLine("");
        // Release the resources of stream object.
        readStream.Close();
        #endregion
        // Release the resources of response object.
        webRep.Close();
    }


你可能感兴趣的:(request,url,短信)