public static string PostURL(string uri, List<KeyValuePair<string, string>> pairs)
var content = new FormUrlEncodedContent(pairs);
var httpClient = new HttpClient();
var response = httpClient.PostAsync(uri, content).Result;
if (response.IsSuccessStatusCode)
return response.Content.ReadAsStringAsync().Result.ToString();
return "Failed";
catch(AggregateException e)
return e.Message;
}
这是调用HttpClient Post的代码位
public ActionResult JobDetails()
var response = WebServicePost.PostURL(WebServicePost.Uri + "JobService.svc/GetJob/", new List<KeyValuePair<String, String>>
new KeyValuePair<string, string>("JobID", "46"),
new KeyValuePair<string, string>("CompanyCode", System.Web.HttpContext.Current.Request.Cookies["CompanyCode"].Value)
List<Job> list = (List<Job>)JsonConvert.DeserializeObject(response.ToString(), typeof(List<Job>));
return View(list);
}
这将填充前端的a DataTable。
对web服务的调用如下。
public List<Job> GetJob(Stream streamData)
dynamic jObject = streamReader.ConvertToDynamicJsonObject(streamData);
int jobID = int.Parse(jObject["JobID"]);
using (var session = NHibernateHelper.OpenSession())
using (var transaction = session.BeginTransaction())
var query = session.QueryOver<Job>().Where(x => x.JobID == jobID).List();
return query.ToList();
catch (Exception e)
Logger.log("Exception at GetJob", e.Message);