如何使用带参数的XML Web Services [英] How to consume XML Web Services with Parameters
问题描述
这是我第一次处理Web服务,并且在使用带参数的Web方法时遇到错误.以下是我的代码.
Its my first time to deal with Web Services and I am having errors on consuming Web Methods with parameters. Below is my code.
[WebMethod]
public string HelloWorld()
{
System.Diagnostics.EventLog.WriteEntry("HELLO WORLD INVOKED", "Congratulations, you have just invoked Hello World method of your local Web Service");
return "Hello World";
}
[WebMethod]
public string PostMethod(string test)
{
System.Diagnostics.EventLog.WriteEntry("POST METHOD INVOKED", "Congratulations, you have just invoked Post Method of your local Web Service. Data - " + test.ToUpper());
return test.ToUpper();
}
我调用这些Web方法的方式是通过创建HTTP POST到http://localhost/Service1.asmx/HelloWorld和http://localhost/Service1.asmx/PostMethod
我能够成功调用HelloWorld
方法(我可以通过查看事件日志来验证它),但是在调用PostMethod
时却总是得到Internal Server Error.
,我还尝试了http://localhost/Service1.asmx/PostMethod?测试=值,但仍然没有运气.
我知道可以通过搜索Google来解决此问题,但是由于我是初学者,因此我对搜索的方向并不熟悉.我尽力了,但是没有运气.
感谢您的帮助.
[更新]
这就是我将数据发布到Web服务的方式
The way I am calling these web methods are by creating a HTTP POST to http://localhost/Service1.asmx/HelloWorld and http://localhost/Service1.asmx/PostMethod
I am able to invoke HelloWorld
method successfully(I can verify it by looking at the Event Log) but when calling PostMethod
I always get Internal Server Error.
I also tried doing http://localhost/Service1.asmx/PostMethod?test=value but still no luck.
I know this can be solved by searching Google but since I am a beginner, I am not familiar on which direction to search. I tried though but no luck.
Appreciate your help.
[UPDATE]
This is how I post the data to the web service
XmlDocument _xml = new XmlDocument();
_xml.Load(filePath);
ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertificatePolicy;
WebRequest _request = WebRequest.Create((WebServiceURL));
_request.Credentials = new NetworkCredential(UserName, Password);
_request.Method = "POST";
_request.ContentType = "text/xml";
XmlTextWriter xw = new XmlTextWriter(_request.GetRequestStream(), Encoding.UTF8);
_xml.WriteTo(xw);
xw.Close();
WebResponse response = _request.GetResponse();
response.Close();
推荐答案
看看这些文章.
Calling-webservice-with-parameters-在ASP.NET中 [从BizTalk Server 2004的业务流程中调用带有自定义参数的Web服务 [ http://aspalliance.com/979_Working_with_Web_Services_Using_ASPNET.all [
Take a look these articles.
Calling-webservice-with-parameters-in-ASP.NET[^]
Calling a Web Service with Custom Parameters from an Orchestration in BizTalk Server 2004[^]
http://aspalliance.com/979_Working_with_Web_Services_Using_ASPNET.all[^]
您可以通过ajax调用访问服务方法
You can access service method through ajax calling
//to allow call from script
[System.Web.Script.Services.ScriptService]
public class DummyService : System.Web.Services.WebService
{
[WebMethod]
public string DummyMethod(string id)
{
try
{
//TO DO: method implementation will goes here
return id
}
catch
{
return null;
}
}
[WebMethod]
public DummyModel DummyModelMethod(string id, string name)
{
try
{
//TO DO: method implementation will goes here
return new DummyModel{Id=int.parse(id), Name=name};
}
catch
{
return null;
}
}
}
public class DummyModel
{
public int Id{set;get;}
public int Name{set;get;}
}
Ajax呼叫助手
Ajax calling helper
function jqAjaxPost(webmethod, paramArray, successFn, errorFn) {
//Create list of parameters in the form : {"paramName1":"paramValue1","paramName2":"paramValue2"}
var paramList = '''';
if (paramArray.length > 0) {
for (var i = 0; i < paramArray.length; i += 2) {
if (paramList.length > 0)
paramList += '','';
paramList += ''"'' + paramArray[i] + ''":"'' + paramArray[i + 1] + ''"'';
}
}
paramList = ''{'' + paramList + ''}'';
//Call the page method
.ajax({ type: "POST", url: webmethod, contentType: "application/json; charset=utf-8", data: paramList, dataType: "json", success: successFn, error: errorFn }); }
.ajax({ type: "POST", url: webmethod, contentType: "application/json; charset=utf-8", data: paramList, dataType: "json", success: successFn, error: errorFn }); }
我们可以通过以下方式调用服务方法
we can invoke the service method by following way
<script type="text/javascript">
jqAjaxPost("DummyService.asmx/DummyMethod",
["id", "1"],
function (msg) {
if (msg.d == null) {
alert("service error.");
return;
}
//alert("sucsess");
alert(msg.d);
},
function () {
alert(''service method not found'');
});
jqAjaxPost("DummyService.asmx/DummyModelMethod",
["id", "1", "name", "hoho"],
function (msg) {
if (msg.d == null) {
alert("service error.");
return;
}
//alert("sucsess");
alert(msg.d.Id);
alert(msg.d.Name);
},
function () {
alert(''service method not found'');
});
</script>
这篇关于如何使用带参数的XML Web Services的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!