Guest User

Untitled

a guest
Sep 14th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. How to access secure soap web service basic authentication in Windows Phone 7
  2. at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
  3. at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass2.<EndGetResponse>b__1(Object sendState)
  4. at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)
  5. at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
  6. at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
  7. at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
  8. at System.Delegate.DynamicInvokeOne(Object[] args)
  9. at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
  10. at System.Delegate.DynamicInvoke(Object[] args)
  11. at System.Windows.Threading.Dispatcher.<>c__DisplayClass4.<FastInvoke>b__3()
  12. at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
  13. at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
  14. at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
  15. at System.Delegate.DynamicInvokeOne(Object[] args)
  16. at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
  17. at System.Delegate.DynamicInvoke(Object[] args)
  18. at System.Windows.Threading.DispatcherOperation.Invoke()
  19. at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
  20. at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
  21. at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
  22. at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
  23. at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)
  24.  
  25. using System;
  26. using System.Text.RegularExpressions;
  27. using System.Xml.Serialization;
  28. using System.Collections.ObjectModel;
  29. namespace MyWP7App
  30. {
  31. [XmlRoot("root")]
  32. public class Categories
  33. {
  34. [XmlArray("Categories")]
  35. [XmlArrayItem("Category")]
  36. public ObservableCollection<Category> Collection { get; set; }
  37. }
  38. }
  39. public class Category
  40. {
  41. [XmlAttribute("ID")]
  42. public int ID { get; set; }
  43. [XmlAttribute("SubCategories")]
  44. public int SubCategoriesCount { get; set; }
  45. [XmlElement("Name")]
  46. public string Name { get; set; }
  47. [XmlArray("SubCategories")]
  48. [XmlArrayItem("SubCategory")]
  49. public ObservableCollection<SubCategory> Collection { get; set; }
  50. }
  51.  
  52. using System;
  53. using System.Collections.ObjectModel;
  54. using System.Linq;
  55. using System.Windows;
  56. using System.Windows.Controls;
  57. using System.Xml.Serialization;
  58. using Microsoft.Phone.Controls;
  59. using System.Xml.Linq;
  60.  
  61. namespace MyWP7App
  62. {
  63. public partial class CategoriesPage : PhoneApplicationPage
  64. {
  65. private ObservableCollection<Category> itemsSource;
  66. public ObservableCollection<Category> ItemsSource
  67. {
  68. get
  69. {
  70. return this.itemsSource;
  71. }
  72. set
  73. {
  74. this.itemsSource = value;
  75. }
  76. }
  77.  
  78. private static MyService.MobileServiceSoapClient Service = null;
  79.  
  80.  
  81. public PanoramaMainPage()
  82. {
  83. InitializeComponent();
  84. if (null == ItemsSource)
  85. GetCategories();
  86. else
  87. imtListBox.ItemsSource = this.ItemsSource;
  88. }
  89.  
  90. private void GetCategories()
  91. {
  92. Service = new MyService.MobileServiceSoapClient();
  93.  
  94. // I tried to do the following when the service is secure, but I had the same error:
  95. // Service.ClientCredentials.UserName.UserName = "Username";
  96. // Service.ClientCredentials.UserName.Password = "Password";
  97.  
  98. Service.GetCategoriesCompleted += new EventHandler<MyService.GetCategoriesCompletedEventArgs>(Service_GetCategoriesCompleted);
  99. Service.GetCategoriesAsync();
  100. }
  101. void Service_GetCategoriesCompleted(object sender, MyService.GetCategoriesCompletedEventArgs e)
  102. {
  103. try
  104. {
  105. if (e.Result == null || e.Error != null)
  106. {
  107. MessageBox.Show("There was an error downloading the XML-file!");
  108. }
  109. if (!e.Cancelled)
  110. {
  111. XmlSerializer serializer = new XmlSerializer(typeof(Categories));
  112. XDocument document = XDocument.Parse("<root>" + e.Result.ToString() + "</root>");
  113. Categories arts = new Categories();
  114. arts = (Categories)serializer.Deserialize(document.CreateReader());
  115. this.ItemsSourceListBox = arts.Collection;
  116. imtListBox.ItemsSource = this.Items1Source;
  117. }
  118. }
  119. catch (Exception ex)
  120. {
  121. MessageBox.Show(ex.Message);
  122. }
  123. Service.Abort();
  124. }
  125. }
  126.  
  127. using (OperationContextScope contextScope = new OperationContextScope(svc.InnerChannel))
  128. {
  129. byte[] bc = System.Text.Encoding.UTF8.GetBytes(@"username" + ":" + "password"); // the string passed after basic:
  130. HttpRequestMessageProperty httpProps = new HttpRequestMessageProperty();
  131. httpProps.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(bc);
  132. OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpProps;
  133. // call the service.
  134. }
Add Comment
Please, Sign In to add comment