2012-05-21 34 views
2

我正在尋找一個可以測試WCF rest api調用的C#示例測試應用程序嗎? 我在互聯網上找不到一個。 我有一些wcf端點的登錄憑證,以及我需要通過的。 有人可以指向我的示例測試休息api appln?如何將WCF rest api集成到應用程序中

感謝

+0

你已經能夠生成客戶端服務代理,即,添加Web服務引用到你的項目?請參閱:http://msdn.microsoft.com/en-us/library/bb628649.aspx。 – mellamokb

+0

它不是一個Web服務,它是一個WCF休息API。 – alice7

回答

1

我想用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。

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

這是完美的,但我也有API密鑰。你知道如何利用它。 – alice7

+0

這很好,但我也有API關鍵。你知道如何利用它。我當前的SOAP url是這樣的:https:// url/v1/Details?key = abcd&Id = 402.而當我嘗試使用你的時候,它會給出未經授權的錯誤。 – alice7

+0

你的意思是你有一個查詢字符串 - 'key = abcd&Id = 402'?這應該沒有問題。也許未經授權的錯誤來自服務。你可以檢查配置嗎?它是基本身份驗證,Windows身份驗證?我的例子是使用基本身份驗證,但你的可能會不同。嘗試沒有授權和/或憑據,看看會發生什麼。 –

0

我喜歡做這種使用Fiddler測試(www.fiddler2.net)

+0

我已經使用fiddler或SOAPUI進行了測試,它們正在工作。我只想編寫一個C#測試程序來測試它。 – alice7

相關問題