2025-03-27 15:05:14 +08:00

67 lines
1.8 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//======================================================================
// HttpHelper
// ZMF_Demo.ZMF
//
// Created by Chen Jialuo on 3/2/2017 4:21:55 PM.
// Copyright © 2017 ZHONGMAKEJI. All rights reserved.
// http://www.zhongmakj.com/
//
//======================================================================
using System.Collections;
using System.Collections.Specialized;
using System.Net;
namespace MobileServicePlatform.Common
{
/// <summary>
/// helper for http get/post
/// </summary>
internal sealed class HttpHelper
{
/// <summary>
/// 不需要新建对象,构造函数为私有
/// </summary>
private HttpHelper()
{
;
}
/// <summary>
/// 发送HTTP POST
/// </summary>
/// <param name="url">目标完整URL</param>
/// <param name="param">FORM内容</param>
/// <returns>调用成功则返回接口返回内容失败时返回null</returns>
public static string Post(string url, NameValueCollection param)
{
try
{
using (var client = new WebClient())
{
byte[] result = client.UploadValues(url, "POST", param);
return System.Text.Encoding.Default.GetString(result);
}
}
catch
{
// TODO: HTTP POST请求发生异常错误日志写入
return null;
}
}
public static string Post(string url, Hashtable param)
{
NameValueCollection nvc = new NameValueCollection();
foreach (DictionaryEntry entry in param)
{
nvc.Add(entry.Key.ToString(), entry.Value.ToString());
}
return Post(url,nvc);
}
}
}