php request与post请求,C# 实现HTTP POST请求与接收响应

用C#实现HTTP请求比较容易实现。根据网上的程序修改而成,实现代码如下:

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

/*** 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";

?>

程序运行结果如下:

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值