接口post +json +bean

public ReturnBean<DealBean> getMember(String tagtype, String tag)
{

try
{
String requestUrl = getObjectUrl(SynUrl.getMemberByCondition, new String[] { "vendorId", "posCode", "tagtype", "tag", }, new Object[] { posset.VendorId, posset.PosCode, tagtype,tag}, posset.VendorId);
String result = HttpWebHelper.OpenReadWithHttps(requestUrl.ToString(), null);
ReturnBean<DealBean> rb = (ReturnBean<DealBean>)Newtonsoft.Json.JsonConvert.DeserializeObject(result, typeof(ReturnBean<DealBean>));
return rb;
}
catch (Exception exx)
{
return null;
}
}

private String getObjectUrl(String bizUrl, String[] parameterNames, Object[] parameterValue, long vendorId, String serverUrl = null)
{
if (string.IsNullOrEmpty(serverUrl))
{
Posset posset = SystemConfig.getPosset();
serverUrl = posset.ServerUrl;//先取数据库中的设置
}
if (string.IsNullOrEmpty(serverUrl)) {//如果为空再去默认的设置
serverUrl = SynUrl.ServersUrl;
}
if (!serverUrl.EndsWith("/"))//是否以/结尾
{
serverUrl = serverUrl + bizUrl;
}
else {
serverUrl = serverUrl.TrimEnd('/') + bizUrl;
}
if (!serverUrl.Contains("?"))//是否以?结尾
{
serverUrl = serverUrl + "?";
}
StringBuilder sb = new StringBuilder();
sb.Append(serverUrl);
if (parameterNames != null) {
for (int i = 0; i < parameterNames.Length; i++) {
if (serverUrl.EndsWith("?") && i == 0)//只有以?结束并且i=0的时候&符号才可以不要
{
sb.AppendFormat(parameterNames[i] + "={0}", parameterValue[i]);
}
else{
sb.AppendFormat("&"+parameterNames[i] + "={0}", parameterValue[i]);
}

}
}
string resultStr=sb.ToString();
string queryString = resultStr.Substring(resultStr.IndexOf("?")+1, resultStr.Length-resultStr.IndexOf("?")-1);
Regex oRegex = new Regex(@"[\u4E00-\u9FA5]");
string dealQueryString =oRegex.Replace(queryString, "");//要处理下汉字 因为两边的加密不一样
string vendorstr = Encrypt.Encrypt_MD5(Encrypt.Encrypt_MD5(vendorId+"").ToUpper()).ToUpper();
//----处理签名
string sign = Encrypt.Encrypt_MD5(dealQueryString + vendorstr).ToUpper();
sb.Append("&sign=" + sign);//把签名补上
return sb.ToString();
}

public static string OpenReadWithHttps(string URL, IDictionary<string, string> parameters, int timeout = 60000, string method = "POST", string strEncoding = "utf-8")
{
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
string strPostdata = null;
if (parameters != null && parameters.Count > 0)
{
strPostdata = "";
string press = "";
foreach (KeyValuePair<string, string> kv in parameters)
{
strPostdata += press + kv.Key + "=" + kv.Value;
press = "&";
}
}
request.Method = method;
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/x-www-form-urlencoded";
if (!string.IsNullOrEmpty(strPostdata))
{
byte[] buffer = encoding.GetBytes(strPostdata);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
}
request.Timeout = timeout;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(strEncoding)))
{
return reader.ReadToEnd();
}

}


public static FlightsResponse QueryFlightPost(FlightQueryRequest ReqDTO)
{
FlightsResponse FlightsRespDTO = new FlightsResponse();
try
{
string requestDTO = JsonConvert.SerializeObject(ReqDTO);
MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
HttpContent httpContent = new ObjectContent(ReqDTO, jsonFormatter);
var url = RequestUrl + "/QueryFlight";
var httpClient = new HttpClient(new RequestHandler() { InnerHandler = new HttpClientHandler() });
httpClient.Timeout = TimeSpan.FromMinutes(3);
var responseJson = httpClient.PostAsync(url, httpContent).Result;
FlightsRespDTO = responseJson.Content.ReadAsAsync().Result; }
catch (Exception)
{
FlightsRespDTO = new FlightsResponse();
}
return FlightsRespDTO;
}
上一篇:C++基础学习教程(六)----类编写的前情回想以及项目实战(1)


下一篇:Java基础之理解Annotation