IDE:VS 2010,C#.net WInfomrs
我有两个项目A和B
答:在一个项目中,我正在做UI工作
B:B项目负责数据库功能,逻辑等
我有一个Web服务W并且使用该服务我在项目A中添加了该Web引用,因此App.config在项目A中。
现在我想在项目B中访问和使用WebService,您能否建议有没有办法在项目B中访问它而不添加Web服务引用..
基本上我想将strXMLString,strFileName参数传递给该Web服务
我想在项目B中写的CODE:
MyServiceSoapClient sc = new _MyServiceSoapClient ();
sc.ReceiveXMLByContent("<tag1>text</tag1>", "myTest.xml");
我可以在项目A中编写此代码,但是您可以建议如何在项目B中实现相同的目标。
我尝试通过发送HttpPost来解决此问题,但收到错误500
/*
//stack trace:
at System.Net.HttpWebRequest.GetResponse()
at UseWebServiceWithoutReference.Form1.button11_Click(Object sender, EventArgs e) in
C:\Users \Yogesh\documents\visual studio 2010\Projects\UseWebServiceWithoutReference\UseWebServiceWithoutReference\Form1.cs:line 369
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at UseWebServiceWithoutReference.Program.Main() in c:\users\Yogesh\documents\visual studio 2010\Projects\UseWebServiceWithoutReference\UseWebServiceWithoutReference\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
webMathod:
[WebMethod]
public string ReceiveXMLByContent(string strXMLData, string strXMLFileName)
{
XMLReceiver.Receive(strXMLData, strXMLFileName);
return "Worked";
}
客户端代码
private void button11_Click(object sender, EventArgs e)
{
string data = string.Format("strXMLData={0}&strXMLFileName={1}", "<tag1>text</tag1>", "myTest.xml");
byte[] dataStream = Encoding.UTF8.GetBytes(data);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:50221/MyWebService.asmx/ReceiveXMLByContent");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = dataStream.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(dataStream, 0, dataStream.Length);
newStream.Close();
var reader = new System.IO.StreamReader(request.GetResponse().GetResponseStream());
string dataReturn = reader.ReadToEnd();
Console.WriteLine(dataReturn);
Console.ReadLine();
}
当我传递简单文本而不是
时,你能告诉我错误500背后的问题是什么吗? string data = string.Format("strXMLData={0}&strXMLFileName={1}", "<tag1>text</tag1>", "myTest.xml"); //Not working error 500
string data = string.Format("strXMLData={0}&strXMLFileName={1}", "myTestString", "myTest.xml");//Working ..
答案 0 :(得分:0)
如果我认为您的网络服务方法看起来像这样。
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string TestMethod(string data ,string fileName)
{
return "Hello World";
}
}
然后您可以使用HttpWebRequest来调用Web服务,而无需任何Web引用。
string data = string.Format("data={0}&fileName={1}", "<tag1>text</tag1>", "myTest.xml");
byte[] dataStream = Encoding.UTF8.GetBytes(data);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:63169/Service1.asmx/TestMethod");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = dataStream.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(dataStream, 0, dataStream.Length);
newStream.Close();
var reader = new System.IO.StreamReader(request.GetResponse().GetResponseStream());
string dataReturn = reader.ReadToEnd();
Console.WriteLine(dataReturn);
Console.ReadLine();
答案 1 :(得分:0)
哇 - 停在那儿......
其中一位评论者@Avijit向您提出了一个非常好的问题 - 您为什么要避免使用网络参考?你没有证明这个要求是合理的,而且现在正在非常反对寻找巧妙的解决方案,以便你能够在不使用WebReference的情况下调用方法。
我认为,在进一步深入这一行之前,您需要问问自己,您迫切希望避免使用Web Reference。你真正的问题,至少从我所在的地方来看,不是“我如何在没有Web引用的情况下调用Web服务?”,而是“如何绕过令人讨厌的Web引用属性x ......”
答案 2 :(得分:0)
最后我创建了自己的编码器和解码器: 我在传递服务器之前编码了xml并在服务器上解码了
private string MyHttpDecoder(string encodedXML)
{
// string decoded = encodeXML.Replace("\\u003c", "<").Replace("\\u003e", ">").Replace("\"","");
string decoded = encodedXML.Replace("66", "<").Replace("67", ">").Replace("68", "\"");
return decoded;
}
private string MyHttpEncoder(string XML)
{
string enc = XML.Replace("<", "66").Replace(">", "67").Replace("\"", "68");
return enc;
}
这很有魅力。