HttpWebRequest简单使用

HttpWebRequest简单使用

 摘要

HttpWebRequest类对WebRequest中定义的属性和方法提供支持,也对使用户能够直接与使用HTTP的服务器交互的附加属性和方法提供支持。

创建HttpWebRequest实例时,使用WebRequest.Create方法。如果统一资源标示符的方案是http://或https://,则Create返回HttpWebRequest对象。

GetResponse方法向RequestUri属性中指定的资源发送同步请求,并返回包含该响应的HttpWebResponse。可以使用BeginGetResponse和EndGetResponse方法对资源发出异步请求.

当想要资源发送数据时,GetRequestStream方法返回用于发送数据的Stream对象。BeginGetRequestStream和EndGetRequestStream方法提供发送数据流的异步访问。

发送Get请求
 public static void main()
{
   //建立请求
  HttpWebRequest  request=(HttpWebRequest)WebRequest.Create("");
  //发送请求获取Http响应
  HttpWebRequest   response=(HttpWebRequest)request.GetResponse();
  //获取响应流
  Stream receiveStream =response.GetResponseStream();
  StreamReader reader =new StreamReader(receiveStr)
  reader.ReadToEnd();
}

发送Post请求

 public static void main()
 {
    //建立请求
    HttpWebRequest request =(HttpWebRequest)WebRequest.Create("");
    request.Method="post";
  httpRequest.Headers.Add("Token", "……");
string inputData=Console.ReadLine(); string postData=inputData; ASCIIEncoding encoding = new ASCIIeNCoding(); byte[] byte1= encoding.GetBytes(postData); request.ContentType="application/x-www-form-urlencoded"; Stream newstream= request.GetRequestStream(); newStream.Write(byte1,0,byte1.Length); HttpWebResponse HttpWResp = (HttpWebResponse)httpRequest.GetResponse(); Stream responseStream = HttpWResp.GetResponseStream(); StreamReader sr = new StreamReader(responseStream, Encoding.UTF8); string result = sr.ReadToEnd(); }

 

  public static string GetShortUrl(string url)
        {
            string paramstr = "{\"url\":\"" + url + "\"}";
            string api = "https://dwz.cn/admin/v2/create";
            Stream responseStream = null;
            StreamReader sr = null;
            Stream requestStream = null;
            string result = null;
            byte[] data = Encoding.UTF8.GetBytes(paramstr);
            try
            {
                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(api);
                httpRequest.ContentType = "application/json";
                httpRequest.Method = "POST";
                httpRequest.Headers.Add("Token", "");
                //发送数据
                httpRequest.ContentLength = data.Length;
                requestStream = httpRequest.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Flush();
                //获取响应内容
                HttpWebResponse HttpWResp = (HttpWebResponse)httpRequest.GetResponse();
                responseStream = HttpWResp.GetResponseStream();
                sr = new StreamReader(responseStream, Encoding.UTF8);
                result = sr.ReadToEnd();
            }
            catch (Exception e)
            {

            }
            finally
            {
                if (sr != null)
                    sr.Close();
                if (responseStream != null)
                    responseStream.Close();
                if (requestStream != null)
                    requestStream.Close();
            }
            if (!string.IsNullOrEmpty(result))
            {
                LitJson.JsonData json = LitJson.JsonMapper.ToObject(result);
                return json["ShortUrl"].ToString();
            }
            return url;
        }

 

posted @ 2019-03-15 13:23  wonderfulviews  阅读(252)  评论(0编辑  收藏  举报