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

    ?>
  • 相关阅读:
    CF终于上紫了。。。
    CF567F/51nod2522 上下序列
    bzoj 前100题计划
    CF1110G Tree-Tac-Toe 博弈论、构造
    BZOJ4816 SDOI2017 数字表格 莫比乌斯反演
    UOJ400/LOJ2553 CTSC2018 暴力写挂 边分治、虚树
    Luogu4774 NOI2018 屠龙勇士 ExCRT
    CF1039D You Are Given a Tree 根号分治、二分、贪心
    CF1056E Check Transcription 字符串哈希
    Luogu4345 SHOI2015 超能粒子炮·改 Lucas、数位DP
  • 原文地址:https://www.cnblogs.com/xxaxx/p/2583082.html
Copyright © 2011-2022 走看看