Asp.net 远程服务器返回错误:(500)内部服务器错误。web服务错误soap请求
我想创建一个响应soap请求的web服务。现在我想向服务发送两个参数,即invoiceno和orderno,在webservice中对其进行处理并返回结果。目前,我只是试图将接收到的值返回到客户端,但我一直收到这个错误“远程服务器返回了一个错误:(500)内部服务器错误。”我是soap web服务的新手。请告诉我我做错了什么。或者这是完全错误的Asp.net 远程服务器返回错误:(500)内部服务器错误。web服务错误soap请求,asp.net,web-services,soap,Asp.net,Web Services,Soap,我想创建一个响应soap请求的web服务。现在我想向服务发送两个参数,即invoiceno和orderno,在webservice中对其进行处理并返回结果。目前,我只是试图将接收到的值返回到客户端,但我一直收到这个错误“远程服务器返回了一个错误:(500)内部服务器错误。”我是soap web服务的新手。请告诉我我做错了什么。或者这是完全错误的 namespace WebApplication7 { /// <summary> /// Summary description for
namespace WebApplication7
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public XmlDocument savedata()
{
XmlDocument xmlSoapRequest = new XmlDocument();
Stream receiveStream = HttpContext.Current.Request.InputStream;
receiveStream.Position = 0;
string invoiceno2 = xmlSoapRequest.GetElementById("invoiceno").Value;
string orderno2 = xmlSoapRequest.GetElementById("orderno").Value;
string gg = @" <soap:Envelopexmlns:soap=""http://www.w3.org/2001/12/soap-envelope""soap:encodingStyle=""http://www.w3.org/2001/12/soap-encoding"">
<soap:Body xmlns:m=""http://www.example.org/stock"">
<m:savedata>
<m:invoiceno>" + invoiceno2 + @"</m:invoiceno>
<m:orderno>" + orderno2 + @"</m:orderno>
<m:haha>hahahahahahahha</m:haha>
</m:savedata>
</soap:Body>
</soap:Envelope>";
XmlDocument cv = new XmlDocument();
cv.LoadXml(gg);
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
// Load into XML document
xmlSoapRequest.Load(readStream);
}
// string gg = invoiceno + " hahahahaha " + orderno;
return cv;
}
}
}
命名空间WebApplication7
{
///
///WebService 1的摘要说明
///
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
//[System.Web.Script.Services.ScriptService]
公共类WebService 1:System.Web.Services.WebService
{
[网络方法]
公共XmlDocument savedata()
{
XmlDocument xmlSoapRequest=新的XmlDocument();
Stream receiveStream=HttpContext.Current.Request.InputStream;
receiveStream.Position=0;
字符串invoiceno2=xmlSoapRequest.GetElementById(“invoiceno”).Value;
字符串orderno2=xmlSoapRequest.GetElementById(“orderno”).Value;
字符串gg=@“
“+invoiceno2+@”
“+orderno2+@”
哈哈哈哈
";
XmlDocument cv=新的XmlDocument();
cv.LoadXml(gg);
使用(StreamReader readStream=newstreamreader(receiveStream,Encoding.UTF8))
{
//加载到XML文档中
加载(readStream);
}
//字符串gg=invoiceno+“hahahahaha”+orderno;
返回cv;
}
}
}
这就是我创建用来消费它的客户端
namespace unicommerce_testing
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Execute());
}
public static string Execute()
{
HttpWebRequest request = CreateWebRequest();
XmlDocument soapEnvelopeXml = new XmlDocument();
soapEnvelopeXml.LoadXml(CreateOrEditItemTypeRequestCreator("44", "2500313-125","Watch",20,20,20,56,"This is a test product","Red","Brillier","NA",45000));
using (Stream stream = request.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
using (WebResponse response = request.GetResponse())
{
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
string soapResult = rd.ReadToEnd();
return soapResult;
}
}
}
/// <summary>
/// Create a soap webrequest to [Url]
/// </summary>
/// <returns></returns>
public static HttpWebRequest CreateWebRequest()
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://localhost:1234/test/WebService1.asmx/savedata");
webRequest.Headers.Add(@"SOAP:Action");
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
public static string CreateOrEditItemTypeRequestCreator()
{
string request= @"<soap:Envelopexmlns:soap=""http://www.w3.org/2001/12/soap-envelope""soap:encodingStyle=""http://www.w3.org/2001/12/soap-encoding"">
<soap:Body xmlns:m=""http://www.example.org/stock"">
<m:savedata>
<m:invoiceno>inv1</m:invoiceno>
<m:orderno>od1</m:orderno>
</m:savedata>
</soap:Body>
</soap:Envelope>";
return request;
}
}
}
namespace unicommerce\u测试
{
公共部分类\u默认值:第页
{
受保护的无效页面加载(对象发送方、事件参数e)
{
Write(Execute());
}
公共静态字符串Execute()
{
HttpWebRequest请求=CreateWebRequest();
XmlDocument soapEnvelopeXml=新的XmlDocument();
LoadXml(CreateOreEditItemTypeRequestCreator(“44”,“2500313-125”,“手表”,20,20,20,56,“这是一个测试产品”,“红色”,“明亮”,“NA”,45000));
使用(Stream=request.GetRequestStream())
{
soapEnvelopeXml.Save(流);
}
使用(WebResponse=request.GetResponse())
{
使用(StreamReader rd=newstreamreader(response.GetResponseStream())
{
字符串soapResult=rd.ReadToEnd();
返回结果;
}
}
}
///
///创建指向[Url]的soap webrequest
///
///
公共静态HttpWebRequest CreateWebRequest()
{
HttpWebRequest webRequest=(HttpWebRequest)webRequest.Create(@)http://localhost:1234/test/WebService1.asmx/savedata");
webRequest.Headers.Add(@“SOAP:Action”);
webRequest.ContentType=“text/xml;charset=\”utf-8\”;
webRequest.Accept=“text/xml”;
webRequest.Method=“POST”;
返回webRequest;
}
公共静态字符串CreateOrEditItemTypeRequestCreator()
{
字符串请求=@“
inv1
od1
";
返回请求;
}
}
}
如果您不想使用契约,那么您的方法会自动生成soap,您可以将WebMethod指定为string而不是XmlDocument,然后将xml返回为string,这更简单。关于错误500,这个错误是在本地发生的吗?是的,我在本地托管了Web服务。我最初是在返回字符串。尝试各种方法来解决此问题。尝试将webRequest属性设置为:webRequest.ContentType=“application/xml”;webRequest.Accept=“应用程序/xml”;