Presentation is loading. Please wait.

Presentation is loading. Please wait.

APIWP7.1WP8W8 System.Net.WebClient  System.Net.HttpWebRequest (async only) System.Net.Http.HttpClient (NuGet) Windows.Web.Syndication.SyndicationClient.

Similar presentations


Presentation on theme: "APIWP7.1WP8W8 System.Net.WebClient  System.Net.HttpWebRequest (async only) System.Net.Http.HttpClient (NuGet) Windows.Web.Syndication.SyndicationClient."— Presentation transcript:

1

2

3

4

5

6 APIWP7.1WP8W8 System.Net.WebClient  System.Net.HttpWebRequest (async only) System.Net.Http.HttpClient (NuGet) Windows.Web.Syndication.SyndicationClient  Windows.Web.AtomPub.AtomPubClient  ASMX Web Services WCF Services OData Services

7 using System.Net;... WebClient client; public MainPage() {... client = new WebClient(); client.DownloadStringCompleted += client_DownloadStringCompleted; } void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { this.downloadedText = e.Result; } private void loadButton_Click(object sender, RoutedEventArgs e) { client.DownloadStringAsync(new Uri("http://MyServer/ServicesApplication/rssdump.xml")); }

8 using System.Net; using System.Threading.Tasks;... private async void LoadWithWebClient() { var client = new WebClient(); string response = await client.DownloadStringTaskAsync( new Uri("http://MyServer/ServicesApplication/rssdump.xml")); this.downloadedText = response; }

9 // Following requires HttpClient.Compression NuGet package var handler = new AdvancedREI.Net.Http.Compression.CompressedHttpClientHandler(); // Create the HttpClient HttpClient httpClient = new HttpClient(handler); // To use without compression support (but why do that?), use default HttpClient constructor // without the compression handler: HttpClient httpClient = new HttpClient(); // Optionally, define HTTP headers httpClient.DefaultRequestHeaders.Add("Accept", "application/json"); // Make the call HttpResponseMessage response = await httpClient.GetAsync( "http://services.odata.org/Northwind/Northwind.svc/Suppliers"); response.EnsureSuccessStatusCode(); // Throws exception if bad HTTP status code string responseBodyAsText = await response.Content.ReadAsStringAsync();

10

11

12

13

14 private const int IANA_INTERFACE_TYPE_OTHER = 1; private const int IANA_INTERFACE_TYPE_ETHERNET = 6; private const int IANA_INTERFACE_TYPE_PPP = 23; private const int IANA_INTERFACE_TYPE_WIFI = 71;... string network = string.Empty; // Get current Internet Connection Profile. ConnectionProfile internetConnectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile(); if (internetConnectionProfile != null) // if ‘null’, we are offline. { switch (internetConnectionProfile.NetworkAdapter.IanaInterfaceType) { case IANA_INTERFACE_TYPE_OTHER: cost += "Network: Other"; break; case IANA_INTERFACE_TYPE_ETHERNET: cost += "Network: Ethernet"; break; case IANA_INTERFACE_TYPE_WIFI: cost += "Network: Wifi\r\n"; break; default: cost += "Network: Unknown\r\n"; break; } }

15

16

17 Wire Serialization FormatSize in Bytes ODATA XML73786 ODATA JSON34030 JSON ‘Lite’15540 JSON ‘Lite’ GZip8680

18

19

20

21 private void EnableGZipResponses(DataServiceContext ctx) { ctx.WritingRequest += new EventHandler ( (_, args) => { args.Headers["Accept-Encoding"] = "gzip"; } ); ctx.ReadingResponse += new EventHandler ( (_, args) => { if (args.Headers.ContainsKey("Content-Encoding") && args.Headers["Content-Encoding"].Contains("gzip")) { args.Content = new GZipStream(args.Content); } } ); } Reference: http://blogs.msdn.com/b/astoriateam/archive/2011/10/04/odata-compression-in-windows-phone-7-5-mango.aspxhttp://blogs.msdn.com/b/astoriateam/archive/2011/10/04/odata-compression-in-windows-phone-7-5-mango.aspx

22

23

24

25

26

27

28

29

30 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { var request = HttpWebRequest.Create("http://myServer:15500/NorthwindDataService.svc/Suppliers") as HttpWebRequest; request.Credentials = new Credentials("username", "password"); // override allows domain to be specified request.Accept = "application/json;odata=verbose"; request.BeginGetResponse(new AsyncCallback(GotResponse), request); } Provide your own UI to request the credentials from the user If you store the credentials, encrypt them using the ProtectedData class

31 private void StoreCredentials() { // Convert the username and password to a byte[]. byte[] secretByte = Encoding.UTF8.GetBytes(TBusername.Text + "||" + TBpassword.Text); // Encrypt the username by using the Protect() method. byte[] protectedSecretByte = ProtectedData.Protect(secretByte, null); // Create a file in the application's isolated storage. IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream writestream = new IsolatedStorageFileStream(FilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, file); // Write data to the file. Stream writer = new StreamWriter(writestream).BaseStream; writer.Write(protectedSecretByte, 0, protectedSecretByte.Length); writer.Close(); writestream.Close(); }

32 // Retrieve the protected data from isolated storage. IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream readstream = new IsolatedStorageFileStream(FilePath, System.IO.FileMode.Open, FileAccess.Read, file); // Read the data from the file. Stream reader = new StreamReader(readstream).BaseStream; byte[] encryptedDataArray = new byte[reader.Length]; reader.Read(encryptedDataArray, 0, encryptedDataArray.Length); reader.Close(); readstream.Close(); // Decrypt the data by using the Unprotect method. byte[] clearTextBytes = ProtectedData.Unprotect(encryptedDataArray, null); // Convert the byte array to string. string data = Encoding.UTF8.GetString(clearTextBytes, 0, clearTextBytes.Length);

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52 ProximityDevice device = ProximityDevice.GetDefault(); // Make sure NFC is supported if (device != null) { PeerFinder.TriggeredConnectionStateChanged += OnTriggeredConnectionStateChanged; // Start finding peer apps, while making this app discoverable by peers PeerFinder.Start(); }

53 ProximityDevice device = ProximityDevice.GetDefault(); // Make sure NFC is supported if (device != null) { PeerFinder.TriggeredConnectionStateChanged += OnTriggeredConnStateChanged; // Include the Windows 8 version of our app as possible peer PeerFinder.AlternateIdentities.Add("Windows", "my Win8 appID"); // Start finding peer apps, while making this app discoverable by peers PeerFinder.Start(); }

54 void OnTriggeredConnStateChanged(object sender, TriggeredConnectionStateChangedEventArgs args) { switch (args.State) { case TriggeredConnectState.Listening: // Connecting as host break; case TriggeredConnectState.PeerFound: // Proximity gesture is complete – setting up link break; case TriggeredConnectState.Connecting: // Connecting as a client break; case TriggeredConnectState.Completed: // Connection completed, get the socket streamSocket = args.Socket; break; case TriggeredConnectState.Canceled: // ongoing connection cancelled break; case TriggeredConnectState.Failed: // Connection was unsuccessful break; }

55 PeerFinder.AllowBluetooth = true; PeerFinder.AllowInfrastructure = true;

56

57 Windows.Networking.Proximity.ProximityDevice proximityDevice; long publishedMessageId = -1; private void PublishUriButton_Click(object sender, RoutedEventArgs e) { if (proximityDevice == null) proximityDevice = ProximityDevice.GetDefault(); // Make sure NFC is supported if (proximityDevice != null) { // Stop publishing the current message. if (publishedMessageId != -1) { proximityDevice.StopPublishingMessage(publishedMessageId); } // Publish the new one publishedMessageId = proximityDevice.PublishUriMessage( new Uri("zune:navigate?appid=351decc7-ea2f-e011-854c-00237de2db9e")); }

58

59

60

61

62

63

64

65 http://microsoft.com/msdn www.microsoft.com/learning http://channel9.msdn.com/Events/TechEd http://microsoft.com/technet

66

67

68


Download ppt "APIWP7.1WP8W8 System.Net.WebClient  System.Net.HttpWebRequest (async only) System.Net.Http.HttpClient (NuGet) Windows.Web.Syndication.SyndicationClient."

Similar presentations


Ads by Google