using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SDKCSharp.Common;
using SDKCSharp.Utility;
using SDKCSharp.Response;
namespace SDKCSharp.Client
{
public class OpenRequest
{
private OpenConfig openConfig;
private OpenHttp openHttp;
public OpenRequest(OpenConfig openConfig)
{
this.openConfig = openConfig;
this.openHttp = new OpenHttp(openConfig);
}
///
/// 请求服务器
///
/// url
/// 请求表单信息
/// 请求头
///
public string Request(string url, RequestForm requestForm, Dictionary header)
{
Dictionary form = requestForm.Form;
List files = requestForm.Files;
if (files != null && files.Count > 0)
{
return this.openHttp.PostFile(url, form, header, files);
}
else
{
RequestMethod requestMethod = requestForm.RequestMethod;
if (requestMethod == RequestMethod.GET)
{
string query = this.BuildGetQueryString(form, requestForm.Charset);
if (!string.IsNullOrEmpty(query))
{
url = url + "?" + query;
}
return openHttp.Get(url, header);
}
return this.openHttp.RequestFormBody(url, form, header);
}
}
public string BuildGetQueryString(Dictionary form, Encoding charset)
{
StringBuilder queryString = new StringBuilder();
Dictionary.KeyCollection keys = form.Keys;
int i = 0;
foreach (string keyName in keys)
{
if (i++ > 0)
{
queryString.Append("&");
}
queryString.Append(keyName).Append("=")
.Append(HttpUtility.UrlEncode(form[keyName], charset));
}
return queryString.ToString();
}
protected string CauseException(Exception e)
{
ErrorResponse result = SopSdkErrors.HTTP_ERROR;
return JsonUtil.ToJSONString(result);
}
}
}