Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [WebMethod]
- public string CheckParam(EmployeeDto emp)
- {
- return "";
- }
- public string CallWebMethod(string webServiceURL, string webMethod, Dictionary<string, object> dicParameters)
- {
- try
- {
- byte[] _requestData = this.CreateHttpRequestData(dicParameters);
- string uri = webServiceURL + "/" + webMethod;
- HttpWebRequest _httpRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
- _httpRequest.Method = "POST";
- _httpRequest.KeepAlive = false;
- _httpRequest.ContentType = "application/x-www-form-urlencoded";
- _httpRequest.ContentLength = _requestData.Length;
- _httpRequest.Timeout = 30000;
- HttpWebResponse _httpResponse = null;
- string _response = string.Empty;
- _httpRequest.GetRequestStream().Write(_requestData, 0, _requestData.Length);
- _httpResponse = (HttpWebResponse)_httpRequest.GetResponse();
- System.IO.Stream _baseStream = _httpResponse.GetResponseStream();
- System.IO.StreamReader _responseStreamReader = new System.IO.StreamReader(_baseStream);
- _response = _responseStreamReader.ReadToEnd();
- _responseStreamReader.Close();
- return _response;
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- private byte[] CreateHttpRequestData(Dictionary<string, object> dic)
- {
- StringBuilder _sbParameters = new StringBuilder();
- foreach (string param in dic.Keys)
- {
- _sbParameters.Append(param);//key => parameter name
- _sbParameters.Append('=');
- _sbParameters.Append(dic[param]);//key value
- _sbParameters.Append('&');
- }
- _sbParameters.Remove(_sbParameters.Length - 1, 1);
- UTF8Encoding encoding = new UTF8Encoding();
- return encoding.GetBytes(_sbParameters.ToString());
- }
- Dictionary<string, object> pp = new Dictionary<string, object>() {
- { "emp",new EmployeeDto { Id=1250,Name="Yasn"} },
- };
- return CallWebMethod("http://localhost:17411/Student.asmx", "CheckParam", pp);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement