gpt4 book ai didi

c# - 如何将 WCF rest api 集成到应用程序

转载 作者:太空宇宙 更新时间:2023-11-03 14:01:04 25 4
gpt4 key购买 nike

我正在寻找一个 C# 示例测试应用程序,它可以测试 WCF rest api 调用?我在互联网上找不到。我还有一些需要通过的 wcf 端点的登录凭据。有人可以指出我的示例测试 rest api appln 吗?

谢谢

最佳答案

我想使用 WebClient应该适用于 WCF REST 服务(如果需要,您可以使用凭据、请求 header 等):

private void TestService()
{
try
{
string address = "<WCF REST service address>";
WebClient client = new WebClient();
string userName = "<user>";
string password = "<password>";
ICredentials creds = new NetworkCredential(userName, password);
client.Credentials = creds;
client.Headers[HttpRequestHeader.Authorization] = String.Format("Basic {0}:{1}", userName, password);
client.Headers[HttpRequestHeader.UserAgent] = @"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)";
Stream data = client.OpenRead(address);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
Console.WriteLine(s);
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
if (ex.Response != null)
{
Stream strm = ex.Response.GetResponseStream();
byte[] bytes = new byte[strm.Length];
strm.Read(bytes, 0, (int)strm.Length);
string sResponse = System.Text.Encoding.ASCII.GetString(bytes);
Console.WriteLine(sResponse);
}
}
}

编辑:使用表单例份验证的 Web 应用程序示例。

如果 Web 应用程序允许匿名用户,我之前的示例就可以工作。如果不是(web.config 包含 <deny users="?"/> 并且 IIS 中启用了匿名访问),则需要两个请求:

  1. 请求登录页面进行身份验证;

  2. 对实际服务 url 的请求。

auth cookie 应该传递给第二个请求(我们使用 CookieContainer 对象来实现这一点)。第一次调用使用 POST 方法传递用户名和密码。因为我的 Web 应用程序使用开箱即用的登录控件,所以我需要传递 View 状态、事件验证等。您可以使用 Fiddler 或 Chrome 开发工具从 Web 浏览器获取登录期间传递的表单数据。这是代码:

private static void TestService()
{
try
{
string loginAddress = "<login url>";
string serviceAddress = "<service url>";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(loginAddress);
req.Method = "POST";
string userName = "<user>";
string password = "<password>";
CookieContainer cc = new CookieContainer();
req.CookieContainer = cc;

StringBuilder sb = new StringBuilder();
sb.Append(@"__VIEWSTATE=<viewstate>");
sb.Append(@"&__EVENTVALIDATION=<event validation>");
sb.Append(@"&ctl00$MainContent$LoginUser$UserName={0}&ctl00$MainContent$LoginUser$Password={1}");
sb.Append(@"&ctl00$MainContent$LoginUser$LoginButton=Log In");
string postData = sb.ToString();
postData = String.Format(postData, userName, password);
req.ContentType = "application/x-www-form-urlencoded";
Encoding encoding = new ASCIIEncoding();
byte[] requestData = encoding.GetBytes(postData);
req.ContentLength = requestData.Length;

//write the post data to the request
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(requestData, 0, requestData.Length);
reqStream.Flush();
}

HttpWebResponse response = (HttpWebResponse)req.GetResponse(); //first call (login / authentication)

req = (HttpWebRequest)WebRequest.Create(serviceAddress);
req.CookieContainer = cc; //set the cookie container which contains the auth cookie
response = (HttpWebResponse)req.GetResponse(); //second call, the service request
Stream data = response.GetResponseStream();
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
Console.WriteLine(s);
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
if (ex.Response != null)
{
Stream strm = ex.Response.GetResponseStream();
byte[] bytes = new byte[strm.Length];
strm.Read(bytes, 0, (int)strm.Length);
string sResponse = System.Text.Encoding.ASCII.GetString(bytes);
Console.WriteLine(sResponse);
}
}
}

关于c# - 如何将 WCF rest api 集成到应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10693632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com