[WebService] 使用httpWebrequest 调用并调试WebService
使用httpWebrequest 调用并调试WebService.
首先 使用httpWebrequest 调用WebService 代码:
using System.Net;
using System.IO;
HttpWebRequest mHttpRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8001/***"); mHttpRequest.Timeout = 20000; mHttpRequest.Method = "POST"; mHttpRequest.ContentType = "application/json"; ////mHttpRequest.UseDefaultCredentials = true; ////mHttpRequest.KeepAlive = false; ////mHttpRequest.ProtocolVersion = HttpVersion.Version10; string Json = Program.GetJson(); mHttpRequest.ContentLength = Encoding.UTF8.GetByteCount(Json); byte[] data = Encoding.UTF8.GetBytes(Json); Stream myRequestStream = mHttpRequest.GetRequestStream(); myRequestStream.Write(data, 0, data.Length); myRequestStream.Flush(); myRequestStream.Close(); HttpWebResponse mHttpResponse = null; try { mHttpResponse = (HttpWebResponse)mHttpRequest.GetResponse(); } catch (Exception ex) { Console.WriteLine(ex.Message); } if (mHttpResponse.StatusDescription == "OK") { } else { } mHttpResponse.Close();
代码很简单,网上也有很多实例,在这边列出主要为自己以后使用方便.
下面简单介绍一下如何在本地调试WebService代码,因为本人最近才接触ASP.NET,所以浪费了点时间在这上面,最后还让人指点了一下才算搞明白(虽然网上也有很多的方法,之前也看过,但自己动手做的时候才知道眼高手低.呵呵)
1.首先 打开WebService项目,依次选择 Debug --> Attach To Process...
2. 然后在show出的界面上选择相应的Process to attach...
3. 设置断点, Run客户端程序即可在适当的断点处切换到Webservice代码里.
注意:WebService所在站点在本地需打开.
以上方法限本人在特定项目下的总结,不一定适合所有项目.仅作参考.