Winform Post请求传递Json格式参数的写法

Posted 周大闯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Winform Post请求传递Json格式参数的写法相关的知识,希望对你有一定的参考价值。

注意的是,Json传递需用到Hashtable(哈希表)来添加参数,本人也试过用JObject添加页不行,感觉应该可以的不知道怎么回事,直接上代码,

Hashtable ht = new Hashtable();
ht.Add("agentCode", agentcode);
ht.Add("agentPassword", agentpwd);
ht.Add("body", body);

HttpRequest.DoPost(registerUrl, ht);  //HttpRequest是自定义的一个类

/// <summary> WebService:Post调用
/// </summary>
/// <param name="url">Webservice地址</param>
/// <param name="paramsOfUrl">传入数据</param>
/// <returns>返回结果</returns>
public static string DoPost(string url, Hashtable paramsOfUrl)
{
if (url == null)
{
throw new Exception("WebService地址为空");
}
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

// 编辑并Encoding提交的数据
byte[] data = GetJointBOfParams(paramsOfUrl);

// 发送请求
request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();

// 获得回复
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string result = reader.ReadToEnd();
reader.Close();
return result;
}

/// <summary> 拼接参数串----Get
/// </summary>
/// <param name="paramsOfUrl">参数项</param>
/// <returns>返回字符串</returns>
private static String GetJointSOfParams(Hashtable paramsOfUrl)
{
if (paramsOfUrl == null || paramsOfUrl.Count == 0) return String.Empty;
// 编辑并Encoding提交的数据
StringBuilder sbuilder = new StringBuilder();
int i = 0;
foreach (DictionaryEntry de in paramsOfUrl)
{
string value = ToHttpChar(de.Value.ToString());
if (i == 0)
{
sbuilder.Append(de.Key + "=" + value);
}
else
{
sbuilder.Append("&" + de.Key + "=" + value);
}
i++;
}
return sbuilder.ToString();
}

/// <summary> 拼接参数串----Post
/// </summary>
/// <param name="paramsOfUrl">参数项</param>
/// <returns>返回字节数组</returns>
private static byte[] GetJointBOfParams(Hashtable paramsOfUrl)
{
// 编辑并Encoding提交的数据
String stringJointOfParams = GetJointSOfParams(paramsOfUrl);
byte[] data = new ASCIIEncoding().GetBytes(stringJointOfParams);
return data;
}

/// <summary> 转义特殊字符
/// </summary>
private static string ToHttpChar(string value)
{
value = value.ToString().Replace("+", "%2B");
//value = value.ToString().Replace(" ", "%20");
//value = value.ToString().Replace("/", "%2F");
//value = value.ToString().Replace("?", "%3F");
//value = value.ToString().Replace("%", "%25");
//value = value.ToString().Replace("#", "%23");
//value = value.ToString().Replace("&", "%26");
//value = value.ToString().Replace("=", "%3D");
//value = value.ToString().Replace(@"", "%5C");
//value = value.ToString().Replace(".", "%2E");
//value = value.ToString().Replace(":", "%3A");
return value;
}

 















































































以上是关于Winform Post请求传递Json格式参数的写法的主要内容,如果未能解决你的问题,请参考以下文章

okhttp post用json传递参数

如何将请求参数转为json格式

如何利用jQuery post传递含特殊字符的数据

给url 添加json参数

django 获取post传递的json数据

struts2怎样获取AJAX post请求传递的数据