我正在尋找一個可以測試WCF rest api調用的C#示例測試應用程序嗎? 我在互聯網上找不到一個。 我有一些wcf端點的登錄憑證,以及我需要通過的。 有人可以指向我的示例測試休息api appln?如何將WCF rest api集成到應用程序中
感謝
我正在尋找一個可以測試WCF rest api調用的C#示例測試應用程序嗎? 我在互聯網上找不到一個。 我有一些wcf端點的登錄憑證,以及我需要通過的。 有人可以指向我的示例測試休息api appln?如何將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中啓用),需要兩個請求:
請求到登錄頁面進行驗證;
請求到實際的服務url。
auth cookie應傳遞給第二個請求(我們使用CookieContainer
對象來實現此目的)。 第一次調用使用POST方法傳遞用戶名和密碼。因爲我的Web應用程序使用開箱即用的登錄控制,所以我需要傳遞視圖狀態,事件驗證等。您可以使用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);
}
}
}
你已經能夠生成客戶端服務代理,即,添加Web服務引用到你的項目?請參閱:http://msdn.microsoft.com/en-us/library/bb628649.aspx。 – mellamokb
它不是一個Web服務,它是一個WCF休息API。 – alice7