2017-09-26 41 views
1

我有以下的WebService方法:可以通过custome类到ASMX Web服务作为PARAM

[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); 

但是当我运行代码它返回Below Error。

'The remote server returned an error: (500) Internal Server Error

当我改变Emp参数去另一个像Datatime和行吟诗人它工作正常。我该如何解决这个问题?

+1

的问题是你需要的模型绑定,我有一个类似的问题而回。让我挖出我的问题\代码 –

+0

看看我的问题,这可能会帮助你https://stackoverflow.com/questions/41891573/asp-net-model-binding-webforms –

+0

我的问题是不同的。我不能使用jquery – FullStack

回答

0

更改将WebMethod要像之下,而且,应该整理出来的问题为您

[WebMethod] 
public static string CheckParam(EmployeeDto emp) 
{ 
    return ""; 
}