zoukankan      html  css  js  c++  java
  • HttpWebRequest (http请求) HttpWebResponse(http响应)

    HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择

    HttpWebRequest定义:

    eg: string url="https://www.baidu.com";

    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

    HttpWebRequest httpRequest=(HttpWebRequest)HttpWebRequest.Create(url);

    // 参数赋值

    X509Certificate2 cerCaiShang = new X509Certificate2(p12certfile, cerpassword);
    if (!isHttp)
    {
    httpRequest.ClientCertificates.Add(cerCaiShang);
    }

    httpRequest.Method="POST";

    httpRequest.ContentType="application/json";

    httpRequest.Headers.Add(key,value);

    httpRequest.Refer=null;

    httpRequest.AllowAutoRedirect=true;

    httpRequest.UserAgent="";

    httpRequest.Accept="*/*";

    //  如果请求方式是Http  还需要加上 httpRequest.ServicePoint.Expect100Continue=false;

    //  如果请求方式不是GET,

    // Stream requestStem=httpRequest.GetRequestStream();

    //StreamWriter sw=new StreamWriter(requestStem);

    // sw.Write(body);   // body 为json格式

    //sw.Close();

    // 响应

    HttpWebResponse hhtpResponse=(HttpWebResponse)httpWebRequest.GetResponse();

    Stream receiveStream=httpResponse.GetResponseStream();

    string result=string.empty();

    int statusCode=0;

    using(StreamReader sr=new StreamReader(receiveStream)){

    result=sr.ReadToEnd();

    }

    statusCode=(int)httpResponse.StatusCode;

  • 相关阅读:
    Linux系统介绍(二)文件系统结构
    为Docker Swarm添加调度策略
    Docker 三剑客之 Docker Swarm
    Hadoop中文文档
    hadoop 学习笔记:mapreduce框架详解
    Java进阶-- GUI
    ceph(8)--关于Ceph PGs
    今天在学习NTP时发现了2个网站
    开始学红帽的RHCE课堂有2次课了,要记下自己的学习经历
    Switch能否用string做参数
  • 原文地址:https://www.cnblogs.com/zxdz/p/13336065.html
Copyright © 2011-2022 走看看