zoukankan      html  css  js  c++  java
  • 本地测试IIS,Post调用接口

    最近在学习三种调用接口方式,POST,Socket,Webserivce,今天刚写完POST方式所以就分享下,欢迎高手指点。

    public string strResult = "";
    protected void Page_Load(object sender, EventArgs e)
    {
    MyResponseList("发送是否成功");
    }
    public void MyResponseList(string Charset)
    {


    try
    {
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] byteArray = encoding.GetBytes(Charset);
    //入口地址 可以传参数
    HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri("http://192.168.16.39:808/Default.aspx?num=3&&name=broueli"));
    webReq.Method = "POST";
    webReq.ContentType = "application/x-www-form-urlencoded";
    webReq.ContentLength = byteArray.Length;

    //获取请求对象
    Stream newStream = webReq.GetRequestStream();
    newStream.Write(byteArray, 0, byteArray.Length);//写入参数
    newStream.Close();
    //返回Internet响应
    HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
    string encod = response.ContentEncoding;

    //判断是否获取到编码方式
    if (encod == null || encod.Length < 1)
    {
    encod = "UTF-8";
    }
    //读取流,该流用于读取来自服务器响应体,Encoding 可以直接定义也可以获取
    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encod));
    //从流的当前位置读取到末尾
    strResult = sr.ReadToEnd();
    sr.Close();
    response.Close();
    newStream.Close();

    }
    catch (Exception exp)
    {

    strResult = "错误:" + exp.Message;

    }
    }

  • 相关阅读:
    【H5】01 入门 & 概述
    【Mybatis + Spring】 Mybatis
    【Mybatis】Bonus01 笔记资料
    【Spring】06 Aop切面功能
    【Spring】05 注解开发
    【Spring】04 注解实现自动装配
    【Spring】03 XML配置
    【Spring】02 过程分析
    【Mybatis】11 注解的使用
    【Mybatis】10 实现分页 & 分页插件
  • 原文地址:https://www.cnblogs.com/bruceli-net/p/4710007.html
Copyright © 2011-2022 走看看