赞
踩
第一步创建名为Post.cs 的类
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
namespace Test.Helper
{
public class Post
{
public static string Postring(string url, Dictionary<string, string> dic)
{
string result = "";
//url = "http://172.16.22.15:8000/" + url;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.Proxy = null;
req.KeepAlive = false;
#region 添加Post 参数
StringBuilder builder = new StringBuilder();
int i = 0;
foreach (var item in dic)
{
if (i > 0)
builder.Append("&");
builder.AppendFormat("{0}={1}", item.Key, item.Value);
i++;
}
byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
#endregion
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取响应内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
return result;
}
}
}
第二步使用:
string results = Post.Postring("api/login", new Dictionary<string, string>
{
{"Account","admin"},
{"Password","123456" },
});
第三步:得到请求数据返回结果(string)
如有问题请私信我
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。