C# 实现HTTP POST请求与接收响应
2015-12-30 23:06
393 查看
用C#实现HTTP请求比较容易实现。根据网上的程序修改而成,实现代码如下:
为了测试,自己搭了一个简单的WEB服务器,使用PHP编程。代码如下:
程序运行结果如下:
工程下载:点击打开链接
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; using System.IO; namespace HTTP_Request_V1 { public partial class Http_Post : Form { public Http_Post() { InitializeComponent(); } private void btn_Go_Click(object sender, EventArgs e) { string postData = "username=" + txt_User.Text + "&password=" + txt_Pwd.Text; postData += ("&vcode=" + txt_VCode.Text); byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData); string url = txt_Url.Text; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); webRequest.Method = "POST"; //POST webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.ContentLength = byteArray.Length; Stream newStream = webRequest.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length); newStream.Close(); HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); txt_Response.Text = sr.ReadToEnd(); } private void Form1_Load(object sender, EventArgs e) { txt_Url.Text = "http://fsmd.vicp.net:22279/PHP_001/hello2/index5.php"; txt_User.Text = "Boy"; txt_Pwd.Text = "12345"; txt_VCode.Text = "admin"; } } }
为了测试,自己搭了一个简单的WEB服务器,使用PHP编程。代码如下:
index5.php
<?php header("content-Type: text/html; charset=Utf-8"); /** * Created by PhpStorm. * User: Administrator * Date: 2015/12/5 * Time: 21:37 */ @$user = $_POST['username']; @$pass =$_POST['password']; @$vcode=$_POST['vcode']; echo "用户名=$user"; echo "|密码=$pass"; echo "|验证码=$vcode"; ?>
程序运行结果如下:
工程下载:点击打开链接
相关文章推荐
- 网络笔记
- http-启用telnet并开启回显功能
- OkHttp的使用
- 请求网络下载图片2(读取是否存在缓存图片)
- 请求网络下载图片
- HTTPS
- HTTP协议详解
- HTTPS工作原理
- 运输层—TCP的拥塞控制
- 浅谈移动端网络优化
- Linux如何挂载网络硬盘
- linux环境下实现svn服务器支持http协议
- 【网络协议】数据链路层
- linux虚拟机与win主机之间ping通以及其他网络设置
- hdu3416 Marriage Match IV(最短路+网络流)
- Asp.Net WebAPI 通过HttpContextBase获取请求参数
- Android 各大网络http请求库的比较及实战
- OS安全系列之一:HTTPS2
- HTTP协议总结
- Ubuntu网络频繁掉线解决方案