• HTTP相关工具类/协助类分享


    usingSystem;
    usingSystem.Collections.Generic;
    usingSystem.IO;
    usingSystem.Linq;
    usingSystem.Net;
    usingSystem.Text;
    namespaceSunCreate.CombatPlatform.Server.ComLib
    {
    ///<summary>
    ///Http上传下载文件
    ///</summary>
    publicclassHttpUtil
    {
    #regioncookie设置
    privatestaticCookieContainerm_Cookie=newCookieContainer();
    publicstaticvoidSetHttpCookie(CookieContainercookie)
    {
    m_Cookie=cookie;
    }
    #endregion
    #regionHttpDownloadFile下载文件
    publicstaticMemoryStreamHttpDownloadFile(stringurl)
    {
    //设置参数
    HttpWebRequestrequest=WebRequest.Create(url)asHttpWebRequest;
    request.Method=”GET”;
    request.CookieContainer=m_Cookie;
    //发送请求并获取相应回应数据
    HttpWebResponseresponse=request.GetResponse()asHttpWebResponse;
    //直到request.GetResponse()程序才开始向目标网页发送Post请求
    StreamresponseStream=response.GetResponseStream();
    //创建写入流
    MemoryStreamstream=newMemoryStream();
    byte[]bArr=newbyte[1024];
    intsize=responseStream.Read(bArr,0,(int)bArr.Length);
    while(size>0)
    {
    stream.Write(bArr,0,size);
    size=responseStream.Read(bArr,0,(int)bArr.Length);
    }
    stream.Seek(0,SeekOrigin.Begin);
    responseStream.Close();
    returnstream;
    }
    #endregion
    #regionHttpUploadFile上传文件
    ///<summary>
    ///Http上传文件
    ///</summary>
    publicstaticstringHttpUploadFile(stringurl,byte[]bArr,stringfileName)
    {
    //设置参数
    HttpWebRequestrequest=WebRequest.Create(url)asHttpWebRequest;
    CookieContainercookieContainer=newCookieContainer();
    request.CookieContainer=cookieContainer;
    request.AllowAutoRedirect=true;
    request.Method=”POST”;
    stringboundary=DateTime.Now.Ticks.ToString(“X”);//随机分隔线
    request.ContentType=”text/plain;charset=utf-8″;
    request.CookieContainer=m_Cookie;
    StreampostStream=request.GetRequestStream();
    postStream.Write(bArr,0,bArr.Length);
    postStream.Close();
    //发送请求并获取相应回应数据
    HttpWebResponseresponse=request.GetResponse()asHttpWebResponse;
    //直到request.GetResponse()程序才开始向目标网页发送Post请求
    Streaminstream=response.GetResponseStream();
    StreamReadersr=newStreamReader(instream,Encoding.UTF8);
    //返回结果网页(html)代码
    stringcontent=sr.ReadToEnd();
    returncontent;
    }
    #endregion
    #regionHttpPost
    ///<summary>
    ///HttpPost
    ///</summary>
    publicstaticstringHttpPost(stringurl,stringdata)
    {
    byte[]bArr=ASCIIEncoding.UTF8.GetBytes(data);
    //设置参数
    HttpWebRequestrequest=WebRequest.Create(url)asHttpWebRequest;
    request.CookieContainer=m_Cookie;
    request.Method=”POST”;
    request.ContentType=”text/plain;charset=utf-8″;
    request.ContentLength=bArr.Length;
    StreampostStream=request.GetRequestStream();
    postStream.Write(bArr,0,bArr.Length);
    postStream.Close();
    //发送请求并获取相应回应数据
    HttpWebResponseresponse=request.GetResponse()asHttpWebResponse;
    //直到request.GetResponse()程序才开始向目标网页发送Post请求
    Streaminstream=response.GetResponseStream();
    StreamReadersr=newStreamReader(instream,Encoding.UTF8);
    //返回结果网页(html)代码
    stringcontent=sr.ReadToEnd();
    returncontent;
    }
    #endregion
    #regionHttpPost
    ///<summary>
    ///HttpPost
    ///</summary>
    publicstaticstringHttpPost(stringurl)
    {
    //设置参数
    HttpWebRequestrequest=WebRequest.Create(url)asHttpWebRequest;
    request.CookieContainer=m_Cookie;
    request.Method=”POST”;
    request.ContentType=”text/plain;charset=utf-8″;
    //发送请求并获取相应回应数据
    HttpWebResponseresponse=request.GetResponse()asHttpWebResponse;
    //直到request.GetResponse()程序才开始向目标网页发送Post请求
    Streaminstream=response.GetResponseStream();
    StreamReadersr=newStreamReader(instream,Encoding.UTF8);
    //返回结果网页(html)代码
    stringcontent=sr.ReadToEnd();
    returncontent;
    }
    #endregion
    #regionHttpGet
    ///<summary>
    ///HttpGet
    ///</summary>
    publicstaticstringHttpGet(stringurl)
    {
    //设置参数
    HttpWebRequestrequest=WebRequest.Create(url)asHttpWebRequest;
    request.CookieContainer=m_Cookie;
    request.Method=”GET”;
    request.ContentType=”text/plain;charset=utf-8″;
    //发送请求并获取相应回应数据
    HttpWebResponseresponse=request.GetResponse()asHttpWebResponse;
    //直到request.GetResponse()程序才开始向目标网页发送Post请求
    Streaminstream=response.GetResponseStream();
    StreamReadersr=newStreamReader(instream,Encoding.UTF8);
    //返回结果网页(html)代码
    stringcontent=sr.ReadToEnd();
    returncontent;
    }
    #endregion
    }
    }

    来源:志在指尖

  • 相关阅读:
    使用sshpass方式实现ssh自动登陆
    MySQL高可用负载均衡
    linux 查看文件夹下的文件个数(当前目录的文件数)
    centos6.5 rsync+inotify实现服务器之间文件实时同步
    Linux shell之数组
    Shell脚本 $0, $#, $*, $@, $?, $$等变量说明
    Linux输出重定向>和>>区别
    EXCEL操作实用学习笔记
    mysql的学习笔记-视图
    mysql学习笔记-索引
  • 原文地址:https://www.cnblogs.com/hite/p/12889067.html
Copyright © 2020-2023  润新知