• C# wnform 请求http ( get , post 两种方式 )


    1.Get请求

    string strURL = "http://localhost/WinformSubmit.php?tel=11111&name=张三";
    System.Net.HttpWebRequest request;
    // 创建一个HTTP请求
    request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
    //request.Method="get";
    System.Net.HttpWebResponse response;
    response
    = (System.Net.HttpWebResponse)request.GetResponse();
    System.IO.StreamReader myreader
    = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8);
    string responseText = myreader.ReadToEnd();
    myreader.Close();
    MessageBox.Show(responseText);

    2.Post请求

    string strURL = "http://localhost/WinformSubmit.php";
    System.Net.HttpWebRequest request;
    request
    = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
    //Post请求方式
    request.Method = "POST";
    // 内容类型
    request.ContentType = "application/x-www-form-urlencoded";
    // 参数经过URL编码
    string paraUrlCoded = System.Web.HttpUtility.UrlEncode("keyword");
    paraUrlCoded
    += "=" + System.Web.HttpUtility.UrlEncode("多月");
    byte[] payload;
    //将URL编码后的字符串转化为字节
    payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
    //设置请求的 ContentLength
    request.ContentLength = payload.Length;
    //获得请 求流
    System.IO.Stream writer = request.GetRequestStream();
    //将请求参数写入流
    writer.Write(payload, 0, payload.Length);
    // 关闭请求流
    writer.Close();
    System.Net.HttpWebResponse response;
    // 获得响应流
    response = (System.Net.HttpWebResponse)request.GetResponse();
    System.IO.StreamReader myreader
    = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8);
    string responseText = myreader.ReadToEnd();
    myreader.Close();
    MessageBox.Show(responseText);

    注:System.Web.HttpUtility.UrlEncode("多月"); 需要引用 System.web.dll

    WinformSubmit.php 代码如下:

    <?php

    header("content-Type: text/html; charset=Utf-8");
    echo mb_convert_encoding("123abc娃哈哈", "UTF-8", "GBK");

    echo "/n------/n";

    foreach($_POST as $key => $value){
    echo $key . '--' .$value ."/n";
    }

    echo "/n-------/n";

    foreach($_GET as $key => $value){
    echo $key . '--' .$value ."/n";
    }

    ?>
  • 相关阅读:
    Mysql配置文件模板
    shell命令记录
    SuSE Linux Enterprise Server
    安装jdk1.8
    云南-第一个应用节点-ssh登录-卡顿的问题
    Python重新安装pip
    Centos6.5修改镜像为国内的阿里云源
    supervisord.conf
    Pandas连接Mysql数据库
    Vim速查命令简版
  • 原文地址:https://www.cnblogs.com/xxaxx/p/2583082.html
Copyright © 2020-2023  润新知