如何将 WCF rest api 集成到应用程序中

本文关键字:集成 应用程序 api rest WCF | 更新日期: 2023-09-27 18:34:48

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

谢谢

如何将 WCF rest api 集成到应用程序中

我认为使用WebClient应该适用于WCF REST服务(如果需要,可以使用凭据,请求标头等(:

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 的请求。

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

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);
        }
    }
}

我更喜欢用Fiddler(www.fiddler2.net(做这种测试