我已经创建了如下的asmx网络服务
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public void GetEmployeesJSONByID(int sId)
{
try
{
string sResult = string.Empty;
List<Employee> lstEmp = new List<Employee>();
List<Employee> lstEmpNew = new List<Employee>();
lstEmp.Add(new Employee { Id = 101, Name = "Nitin", Salary = 10000 });
lstEmp.Add(new Employee { Id = 102, Name = "Dinesh", Salary = 20000 });
switch (sId)
{
case 101:
lstEmpNew.Add(new Employee { Id = 101, Name = "Nitin", Salary = 10000 });
break;
case 102:
lstEmpNew.Add(new Employee { Id = 102, Name = "Dinesh", Salary = 20000 });
break;
default:
break;
}
JavaScriptSerializer js = new JavaScriptSerializer();
sResult = js.Serialize(lstEmpNew);
Context.Response.Write(sResult);
}
catch (Exception ex)
{
Context.Response.Write(ex.Message.ToString());
}
}
我想在C#中使用此Web服务。所以我用下面的代码
string url = http://XXXXXX/SampleWebService/Service.asmx/GetEmployeesJSONByID;
HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(url);
webreq.Headers.Clear();
webreq.Method = "GET";
Encoding encode = Encoding.GetEncoding("utf-8");
HttpWebResponse webres = null;
webres = (HttpWebResponse)webreq.GetResponse();
Stream reader = null;
reader = webres.GetResponseStream();
StreamReader sreader = new StreamReader(reader, encode, true);
string result = sreader.ReadToEnd();
sOutput = result;
如何从C#中将此sId作为参数传递来测试此Web服务?
答案 0 :(得分:0)
要使用网络服务,请右键单击您的项目,然后选择“添加服务参考”并添加您的网址
http://XXXXXX/SampleWebService/Service.asmx
它将在您的项目中创建代理类。然后,在后面的代码中创建该代理类的实例,并将必需的参数传递给调用方法。例如
ClsWebserviceProxy objProxy = new ClsWebserviceProxy();
objProxy.GetEmployeesJSONByID(5);
要使用动态网址,您可以尝试以下代码,
public void add()
{
var _url = "http://www.dneonline.com/calculator.asmx";
var _action = "http://www.dneonline.com/calculator.asmx?op=Add";
string methodName = "Add";
XmlDocument soapEnvelopeXml = CreateSoapEnvelope(methodName);
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
// suspend this thread until call is complete. You might want to
// do something usefull here like update your UI.
asyncResult.AsyncWaitHandle.WaitOne();
// get the response from the completed web request.
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
Console.Write(soapResult);
}
}
private static HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", action);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
private static XmlDocument CreateSoapEnvelope(string methodName)
{
XmlDocument soapEnvelopeDocument = new XmlDocument();
string soapStr =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<{0} xmlns=""http://tempuri.org/"">
{1}
</{0}>
</soap:Body>
</soap:Envelope>";
string postValues = "";
Dictionary<string, string> Params = new Dictionary<string, string>();
//< "name" > Name of the WebMethod parameter (case sensitive)</ param >
//< "value" > Value to pass to the paramenter </ param >
Params.Add("intA", "5"); // Add parameterName & Value to dictionary
Params.Add("intB", "10");
foreach (var param in Params)
{
postValues += string.Format("<{0}>{1}</{0}>", param.Key, param.Value);
}
soapStr = string.Format(soapStr, methodName, postValues);
soapEnvelopeDocument.LoadXml(soapStr);
return soapEnvelopeDocument;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
try
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
catch (Exception ex)
{
}
}