HttpWebRequest UserAgent setter - 此类未实现此属性

时间:2014-08-25 10:58:31

标签: c# facebook windows-phone-7

我正在开发一个简单的应用程序。我正在尝试在我的应用程序中实现脸书。我已经完成了一个示例项目。但是当我尝试在原始项目中实现时,我收到错误This property is not implemented by this class.

public class FacebookLoginPageViewModel
{
    private static WebBrowser _webBrowser;
    private Page _page;
    private const string ExtendedPermissions = "user_about_me,read_stream,publish_stream,user_birthday,offline_access,email";
    private readonly FacebookClient _fb = new FacebookClient();
    private const string AppId = "1XXX58XXXXXXXX9";
    Uri url;
    public FacebookLoginPageViewModel(Panel container, Page page)
    {
        _page = page;
        _webBrowser = new WebBrowser();

        var loginUrl = GetFacebookLoginUrl(AppId, ExtendedPermissions);
        url = loginUrl;
        container.Children.Add(_webBrowser);
        _webBrowser.Navigated += webBrowser_Navigated;
        _webBrowser.Navigate(loginUrl);
    }

    private Uri GetFacebookLoginUrl(string appId, string extendedPermissions)
    {
        var parameters = new Dictionary<string, object>();
        parameters["client_id"] = appId;
        parameters["redirect_uri"] = "https://www.facebook.com/connect/login_success.html";
        parameters["response_type"] = "token";
        parameters["display"] = "touch";

        // add the 'scope' only if we have extendedPermissions.
        if (!string.IsNullOrEmpty(extendedPermissions))
        {
            // A comma-delimited list of permissions
            parameters["scope"] = extendedPermissions;
        }
        return _fb.GetLoginUrl(parameters);
    }

    void webBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        FacebookOAuthResult oauthResult;
        if (!_fb.TryParseOAuthCallbackUrl(e.Uri, out oauthResult))
        {
            return;
        }

        if (oauthResult.IsSuccess)
        {
            var accessToken = oauthResult.AccessToken;
            LoginSucceded(accessToken);
        }
        else
        {
            // user cancelled
            MessageBox.Show(oauthResult.ErrorDescription);
        }
    }

    private void LoginSucceded(string accessToken)
    {
        try
        {
            var fb = new FacebookClient(accessToken);

            fb.GetCompleted += (o, e) =>
            {
                if (e.Error != null)
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show(e.Error.Message);
                        return;
                    });
                }

                var result = (IDictionary<string, object>)e.GetResultData();
                var id = (string)result["id"];

                LoadFacebookData(accessToken, id);
                var url = string.Format("/com/beno/WP7Client/views/FacebookLoginPage.xaml?access_token={0}&id={1}", accessToken, id);
                var rootFrame = (App.Current as App).RootFrame;
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    rootFrame.Navigate(new Uri(url, UriKind.Relative));
                });
            };

            fb.GetAsync("me?fields=id");
        }
        catch(Exception ex)
        {
            Console.WriteLine("error.."+ex.ToString());
        }
    }

fb.GetAsync("me?fields=id");我收到错误

System.NotImplementedException: This property is not implemented by this class.
   at System.Net.HttpWebRequest.SetUserAgent(String value)
   at System.Net.HttpWebRequest.set_UserAgent(String value)
   at Facebook.HttpWebRequestWrapper.set_UserAgent(String value)
   at Facebook.HttpWebRequestWrapper.TrySetUserAgent(String userAgent)
   at Facebook.FacebookClient.PrepareRequest(HttpMethod httpMethod, String path, Object parameters,
  Type resultType, Stream& input, Boolean& containsEtag, IList`1& batchEtags)
   at Facebook.FacebookClient.ApiAsync(HttpMethod httpMethod, String path, Object parameters, Type resultType, Object userState)
   at Facebook.FacebookClient.GetAsync(String path, Object parameters, Object userState)
   at Facebook.FacebookClient.GetAsync(String path)
   at TestReactive.com.beno.WP7Client.ViewModels.FacebookViewModel.LoginSucceded(String accessToken)
   at TestReactive.com.beno.WP7Client.ViewModels.FacebookViewModel.webBrowser_Navigated(Object sender, NavigationEvPID:0ED1004E TID:0F8A0042 entArgs e)
   at Microsoft.Phone.Controls.WebBrowser.FireNavigatedEvent(Uri uri)
   at Microsoft.Phone.Controls.WebBrowserInterop.ReversePInvokeThunk.OnNavigated(Int32 webBrowserControlId, String uri)

我感到困惑。为什么它在一个项目中工作?是的,它不适用于另一个项目? 请让我解决这个问题。

2 个答案:

答案 0 :(得分:0)

当我尝试向标头添加WebHeaderCollection时,我遇到了类似的错误。在分配这些标头期间,例外。错误消息&#34;此属性未由此类实现,&#34;会被抛出。

解决方法,发现我在WebRequest实例化时提供的URL中存在错误。

简称:我的网络请求中格式错误的网址导致了错误#34;此属性未由此类实现&#34;将标题添加到集合时抛出。修复URL修复了错误。

答案 1 :(得分:-1)

尝试禁用在Visual Studio中捕获抛出的CLR异常。

在DEBUG菜单Exceptions ...和未选中的Common Language Run-time Exceptions。

我怀疑在原始示例项目中已禁用此功能。我认为Facebook库内部捕获并处理此异常,因此您无需因此而闯入调试器。即使上面的内容未经检查,未捕获的异常仍然会在VS中触发相应的消息,因此设置的更改应该是无害的。