Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. [WebMethod]
  2. public string CheckParam(EmployeeDto emp)
  3. {
  4. return "";
  5. }
  6.  
  7. public string CallWebMethod(string webServiceURL, string webMethod, Dictionary<string, object> dicParameters)
  8. {
  9. try
  10. {
  11. byte[] _requestData = this.CreateHttpRequestData(dicParameters);
  12.  
  13. string uri = webServiceURL + "/" + webMethod;
  14. HttpWebRequest _httpRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
  15.  
  16.  
  17. _httpRequest.Method = "POST";
  18. _httpRequest.KeepAlive = false;
  19. _httpRequest.ContentType = "application/x-www-form-urlencoded";
  20.  
  21. _httpRequest.ContentLength = _requestData.Length;
  22. _httpRequest.Timeout = 30000;
  23. HttpWebResponse _httpResponse = null;
  24. string _response = string.Empty;
  25.  
  26. _httpRequest.GetRequestStream().Write(_requestData, 0, _requestData.Length);
  27. _httpResponse = (HttpWebResponse)_httpRequest.GetResponse();
  28. System.IO.Stream _baseStream = _httpResponse.GetResponseStream();
  29. System.IO.StreamReader _responseStreamReader = new System.IO.StreamReader(_baseStream);
  30. _response = _responseStreamReader.ReadToEnd();
  31. _responseStreamReader.Close();
  32.  
  33. return _response;
  34. }
  35. catch (Exception ex)
  36. {
  37. throw new Exception(ex.Message);
  38. }
  39. }
  40.  
  41. private byte[] CreateHttpRequestData(Dictionary<string, object> dic)
  42. {
  43. StringBuilder _sbParameters = new StringBuilder();
  44. foreach (string param in dic.Keys)
  45. {
  46. _sbParameters.Append(param);//key => parameter name
  47. _sbParameters.Append('=');
  48. _sbParameters.Append(dic[param]);//key value
  49. _sbParameters.Append('&');
  50. }
  51. _sbParameters.Remove(_sbParameters.Length - 1, 1);
  52.  
  53. UTF8Encoding encoding = new UTF8Encoding();
  54.  
  55. return encoding.GetBytes(_sbParameters.ToString());
  56.  
  57. }
  58.  
  59. Dictionary<string, object> pp = new Dictionary<string, object>() {
  60. { "emp",new EmployeeDto { Id=1250,Name="Yasn"} },
  61.  
  62. };
  63.  
  64. return CallWebMethod("http://localhost:17411/Student.asmx", "CheckParam", pp);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement