在.NET的Windows桌面應用中使用Amazon的Web Services (轉)
摘要:namespace prefix = o ns = "urn:schemas--com::office" />
Amazon免費的 Services可以讓你在自己編寫的應用中查詢並顯示Amazon站點上的資料,接下來我們就進入雅瑪遜,用去體會Web Services。
目錄
1. Amazon Web Services簡介
2. 使用.NET體驗Amazon Web Services
3. 小結
1. Amazon Web Services簡介
幾年前Amazon就釋出了能夠查詢和顯示自己產品資訊的Web Services,這個Web services相容幾乎所有的能夠藉助HTTP並消費HTML或基於P的XML的語言。
在使用Amazon Web Services之前,需要到Amazon開發包,這個開發包包含開發人員需要的文件資料以及程式碼樣例()
之後需要得到Amazon 開發者的證件(https://associates.amazon.com/exec/panama/associates/join/developer/application.html )
2. 使用.NET體驗Amazon Web Services
有兩種方法可以查詢Amazon Web Service:使用HTTP 請求或者藉助SOAP,下面就用強大的.NET來完成這次雅瑪遜之旅。
首先,我們需要建立一個簡單的應用程式,它必須要有一個文字框作為查詢使用,在輸入了關鍵字後程式便Amazon的服務按照輸入的關鍵字查詢,然後將找到的相關產品標題輸出到列表框中,最後當使用者點選了列表中的相應標題時顯示明細到另一個多行文字框中。
完成以上的步驟,我們需要傳送兩種請求:KeyRequest和AsinRequest。ASIN是“Amazon.com標準產品序號”的簡稱,它唯一的標示了每一件Amazon的產品。請求又分為兩種型別:輕量級和重量級,本應用中,KeywordRequest為輕量級請求,而AsinRequest是重量級的請求。這是由請求返回的資料量決定的。
① 建立.NET 應用,並在視窗中加入下列
txtSearch—單行文字框,允許使用者輸入關鍵字進行商品店的查詢
lstResults—列表框,顯示根據關鍵字得到的標題
txtDetails—多行文字框,顯示標題對應的商品明細
② 引入Amazon Web Service
在.NET 的“Project”主選單中選擇“Add Web Reference…”選單項,並在彈出對話方塊的位址列中輸入“”後回車。
① 呼叫Amazon Web Service
引入Web Service成功後,編寫程式碼將Amazon Web Service的名稱空間引入
[]
using AmazonWebServices.com.amazon.soap;
[]
Imports AmazonWebServices.com.amazon.soap
要實現SOAP查詢,首先需要建立一個AmazonSearchService類的例項
[VB.NET]
Dim srch As New AmazonSearchService()
每次關鍵字查詢都會需要建立KeywordRequest 例項;每次ASIN查詢就需要建立一個AsinRequest。每次的請求也會返回一個ProductInfo的XML文件節點, 所以也必須建立一個ProductInfo例項從響應中檢索ProductInfo的資料。
[VB.NET]
Dim kr As New KeywordRequest()
kr.devtag = "你得到的Amazon 開發者的證件"
kr.keyword = txtSearch.Text
kr.mode = "books"
kr.sort = "+titlerank"
kr.tag = "webservices-20"
kr.type = "lite"
kr.page = "1"
Dim pi As ProductInfo = srch.KeywordSearchRequest(kr)
Dim allDetails() As Details = pi.Details
請求發出之前需要將我們之前得到的Amazon 開發者證件的數值付給KeywordRequest 物件的devtag 屬性,而結果會按照標題字母的升序排列。每次查詢最多會返回10條資料,因此想要得到更多的資料必須進行多次查詢。
[VB.NET]
If (pi.TotalResults > 10) Then
kr.page = "2"
pi = srch.KeywordSearchRequest(kr)
allDetails = pi.Details
End If
② 顯示查詢結果
[VB.NET]
Me.Cursor = Cursors.WaitCursor
Dim i As Int16
For i = 0 To allDetails.Length - 1
lstResults.Items.Add("產品名稱:" & allDetails(i).ProductName & " || 產品序號:" & allDetails(i).Asin)
Next
Me.Cursor = Cursors.Default
③ 顯示明細
使用ListBox的edIndd事件檢索ASIN(產品序號)請求的資料。
[VB.NET]
Dim ar As New AsinRequest()
ar.asin = Microsoft.VisualBasic.Right(CStr(lstResults.Items(lstResults.SelectedIndex)), Len(CStr(lstResults.Items(lstResults.SelectedIndex))) - (InStr(1, CStr(lstResults.Items(lstResults.SelectedIndex)), "|| 產品序號:") + Len("|| 產品序號:")) + 1)
ar.devtag = "你得到的Amazon 開發者的證件"
ar.type = "heavy"
ar.tag = "webservices-20"
Dim srch As New AmazonSearchService()
Dim pi As ProductInfo = srch.AsinSearchRequest(ar)
Dim allDetails() As Details = pi.Details
txtDetails.Text = "產品名稱:" & allDetails(0).ProductName & System.Environment.NewLine
txtDetails.Text = txtDetails.Text & "產品序號:" & allDetails(0).Asin & System.Environment.NewLine
Dim strAuthors() As String = allDetails(0).Authors
If strAuthors.Length > 0 Then
Dim j As Int16
For j = 0 To strAuthors.Length - 1
txtDetails.Text = txtDetails.Text & "作者:" & strAuthors(j) & System.Environment.NewLine
Next
End If
txtDetails.Text = txtDetails.Text & "分類:" & allDetails(0).Catalog & System.Environment.NewLine
txtDetails.Text = txtDetails.Text & "出版商:" & allDetails(0). Publisher & System.Environment.NewLine
txtDetails.Text = txtDetails.Text & "數量:" & allDetails(0).CollectibleCount & System.Environment.NewLine
txtDetails.Text = txtDetails.Text & "價格:" & allDetails(0).CollectiblePrice & System.Environment.NewLine
txtDetails.Text = txtDetails.Text & "小圖片URL:" & allDetails(0).ImageUrlSmall & System.Environment.NewLine
txtDetails.Text = txtDetails.Text & "中圖片URL:" & allDetails(0).ImageUrlMedium & System.Environment.NewLine
txtDetails.Text = txtDetails.Text & "大圖片URL:" & allDetails(0).ImageUrlLarge & System.Environment.NewLine
④ 顯示URL指向的圖片
從上面程式碼的最後幾行可以看到每個產品是含有圖片的,我們可以自己編寫一個ImageFromAmazon方法用來顯示Amazon網站上面的圖片資訊。
Private Function ImageFromAmazon(ByVal url As String) As Image
Dim wc As New WebClient()
Dim st As System.IO.Stream = wc.OpenRead(url)
Dim img As Image = Image.FromStream(st)
st.Close()
Return img
End Function
ImageFromAmazon方法建立WebClient例項並從指定的URL請求圖片資料分配給Stream物件,方法的返回值是Image型別,這種型別可以直接賦值給PictureBox的Image屬性。那麼在現有的窗體上面加入PictureBox控制元件,然後編寫如下程式碼放到ListBox的SelectedIndexChanged事件的最後,就能夠實現顯示URL圖片的功能了。
[VB.NET]
pbImage.Image = ImageFromAmazon(allDetails(0).ImageUrlMedium)
3. 小結
這個例子看上去非常簡單,但是透過這個例子可以幫助學習.NET和Web Service。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-998894/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Amazon Web Services (目錄)Web
- MVC應用程式使用Web Services(asmx)MVCWebASM
- 【譯】使用 WebView2 將最好的 Web 帶到 .NET 桌面應用程式中WebView
- IoC在ASP.NET Web API中的應用ASP.NETWebAPI
- 用 AI 讓資料分析更智慧 - Amazon Q 在 Amazon Quicksight 中的應用AIUI
- BurpSuite在非Web應用測試中的應用UIWeb
- Kuuga——轉化任何 Web 頁面為桌面應用的跨平臺工具Web
- Redis在.net中的使用(7)redis部署為Windows服務RedisWindows
- noVNC在Windows下的應用VNCWindows
- Redis在.net中的使用(2).net專案中的Redis使用Redis
- MVC模式在Java Web應用程式中的實現MVC模式JavaWeb
- Redis在Web專案中的應用與實踐RedisWeb
- Redis 在 Web 專案中的應用與實踐RedisWeb
- .Net core 中 AutoMapper的應用APP
- 深入解析:JWT Bearer 認證在 .NET Core 中的應用JWT
- web開發實戰教程:Apache Shiro在web專案中的應用WebApache
- JSON資料格式及其在WEB開發中的應用JSONWeb
- Jetty - 在整合Spring的J2SE應用程式中嵌入Jetty的Web功能(應用和Web共用ApplicationContext)JettySpringWebAPPContext
- 使用 Lambda Web Adapter 在 Lambda 上 構建 web 應用WebAPT
- 在優麒麟上使用 Electron 開發桌面應用
- SpringBoot中的響應式web應用Spring BootWeb
- 手把手教會將 Windows 窗體桌面應用從.NET Framework遷移到 .NET SDK/.NET 6 格式WindowsFramework
- 使用.NET5、Blazor和Electron.NET構建跨平臺桌面應用Blazor
- musl libc 與 glibc 在 .NET 應用程式中的相容性
- 使用 Amazon SageMaker 構建文字摘要應用
- 淺談canvas在web開發中的應用與優化CanvasWeb優化
- Jenkins在Java web專案CI/CD中的簡單應用JenkinsJavaWeb
- 高收益的笨辦法:暴破在Windows提權中的應用Windows
- .NET Web應用中為什麼要使用async/await非同步程式設計WebAI非同步程式設計
- .Net在Windows上使用Jenkins做CI/CD的那些事WindowsJenkins
- 內容定址在 Web3 的應用Web
- 使用Hibernate-Validator優雅的驗證RESTful Web Services的引數RESTWeb
- 8 Apply Services 應用服務APP
- 在Laravel 中如何自定義servicesLaravel
- 使用Electron構建跨平臺的桌面應用
- Ooui:在瀏覽器中執行.NET應用UI瀏覽器
- gRPC在 ASP.NET Core 中應用學習RPCASP.NET
- 在 .NET Core 中應用六邊形架構架構
- 使用 AI 在醫療影像分析中的應用探索AI