zoukankan      html  css  js  c++  java
  • 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
    }
    }

    来源:志在指尖

  • 相关阅读:
    RocketMQ logback使用实践
    用SLF4j/Logback打印日志-3
    UI调试工具 SAK 布局 [MD]
    Gradle 翻译 Merge AndroidManifest 合并清单文件 [MD]
    Gradle 翻译 build dependencies 依赖 [MD]
    Gradle 翻译 tips and recipes 使用技巧 [MD]
    依赖注入 DI 控制反转 IOC 概念 案例 [MD]
    属性动画 基本使用案例 [MD]
    架构 MVC MVP MVVM 简介 [MD]
    MMKV 多进程K-V组件 SP [MD]
  • 原文地址:https://www.cnblogs.com/hite/p/12889067.html
Copyright © 2011-2022 走看看