/// <summary>
/// 调用接口获取信息
/// </summary>
/// <param name="p_strID"></param>
/// <returns></returns>
public Dictionary<string, object> GetInformationService(string p_strID)
{
const string strUrl = "http://www.baidu.com/api/test.php?query=test&moudle=moudleName";
WebServiceMethod webServiceMethod = new WebServiceMethod(strUrl, "");
Dictionary<string, string> dataDic = new Dictionary<string, string>();
dataDic.Add("id", p_strID);
var dataJson = (new JavaScriptSerializer()).Serialize(dataDic);
string secretSign = WebServiceMethod.ConvertJsonToSecretSign(dataJson);
string queryString = "data=" + dataJson + "&secSign=" + secretSign;
var response = webServiceMethod.PostRequest(queryString);
Dictionary<string, object> jsonDesToDic = webServiceMethod.ConvertValueAddResponseToObject(response);
return jsonDesToDic;
}
WebServiceMethod类
//加密方式
......
//查询
public HttpWebResponse PostRequest(string strRequestParams)
{
//建立HttpWebRequest
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(this.WebRequestURL);
//定义网关
if (Proxy != "")
{
WebProxy objProxy = new WebProxy(this.Proxy);
req.Proxy = objProxy;
}
req.Accept = "application/xml";
req.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
req.KeepAlive = false;
req.Method = "POST";
if (req.Method == "POST")
{
byte[] b = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(strRequestParams);
req.ContentLength = b.Length;
try
{
Stream oSRe = req.GetRequestStream();
//添加接口参数到流
oSRe.Write(b, 0, b.Length);
oSRe.Close();
oSRe = null;
}
catch (Exception)
{
req = null;
return null;
}
}
HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
return myResponse;
}