C# 后台请求接口方式

直接上代码:

/// <summary>
/// 后台请求接口方法
/// </summary>
public class myHttpRequest
{
    /// <summary>
    /// get方法调用接口获取json文件内容
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public string GetFunction(string url)
    {
        string serviceAddress = url;//"http://222.111.999.444:8687/tttr/usercrd/12/b7e50cb45a?userid=9999";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
        request.Method = "GET";
        request.ContentType = "text/html;charset=UTF-8";
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream myResponseStream = response.GetResponseStream();
        StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
        string retString = myStreamReader.ReadToEnd();
        myStreamReader.Close();
        myResponseStream.Close();
        //Response.Write(retString);
        return retString;
    }
    //post请求


}

 

 

上一篇:SQL 笔记


下一篇:读取HTTPWebReponse使用GetResponseStream readToEnd返回奇怪的字符