zoukankan      html  css  js  c++  java
  • Get函数及Post函数

    /// <summary>
    /// Get函数
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    private string GetHttp(string url)
    {
    string content = "";
    try
    {
    System.Net.HttpWebRequest myRequest = (System.Net.HttpWebRequest)WebRequest.Create(url);
    myRequest.Method = "GET";
    HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
    System.IO.StreamReader reader = new System.IO.StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
    content = reader.ReadToEnd();
    reader.Close();
    }
    catch (Exception ex)
    {
    return ex.Message;
    }
    return content;
    }


    /// <summary>
    /// Post函数
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public static string PostHttp(string url, string body)
    {
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

    httpWebRequest.ContentType = "application/x-www-form-urlencoded";
    httpWebRequest.Method = "POST";
    httpWebRequest.Timeout = 20000;

    byte[] btBodys = Encoding.UTF8.GetBytes(body);
    httpWebRequest.ContentLength = btBodys.Length;
    httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);

    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
    string responseContent = streamReader.ReadToEnd();

    httpWebResponse.Close();
    streamReader.Close();
    httpWebRequest.Abort();
    httpWebResponse.Close();

    return responseContent;
    }

  • 相关阅读:
    基础网络技术--学习网络的的道路漫长啊
    华为nova8se和vivoS7e的区别哪个好
    Java.awt实现一个简单的围棋
    HashMap put原理详解(基于jdk1.8)
    Paper Pal:一个中英文论文及其代码大数据搜索平台
    【u116】最短路计数
    【u108】取数游戏
    【u106】3D模型
    【topcoder SRM 652 DIV2 250】ValueOfString
    【u103】绘制二叉树
  • 原文地址:https://www.cnblogs.com/wangnengduo/p/7058738.html
Copyright © 2011-2022 走看看