为什么这种方法被调用两次,并且在第一次返回时不起作用?

时间:2014-08-28 11:12:02

标签: c# httpwebrequest httpwebresponse amf

Flash应用程序使用AMF消息格式调用方法SyncAddressBook(c#),在此方法中,它使用HttpWebRequest(post和get)调用其他两个WebApis。

但是只有在调用WebApi(方法SyncAddressBook)时,该方法才能很好地返回flash,当调用WebApi(方法SyncAddressBook)时,方法SyncAddressBook无法返回到调用它的那个(我没有在这里写这个方法)而是,它返回自己。并且第二次,SyncAddressBook方法返回错误闪烁。

这是方法(我省略了一些东西)

    public AMFGreetoAddressBookResult SyncAddressBook(AMFGreetoLogin loginArgs, AMFGreetoAddressBook addressBook)
    {
             var result = new ........
            this.PostReceiversGroups(addressBook, accessKey);
            this.GetReceivers(accessKey, out receivers);
             result.ResultCode = true;
            return result;
     }


    private Boolean PostReceiversGroups(AMFGreetoAddressBook addressBook, string accessKey)
    {
        Uri theUri = new Uri(athenaSiteUrl + "/api/receivers");
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(theUri);
        httpWebRequest.KeepAlive = false;
        httpWebRequest.Accept = "application/json";
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";
        httpWebRequest.Headers.Add(AuthenticationServiceCodeHeaderKey, AddressServiceAccessCode);
        httpWebRequest.Headers.Add(uthenticationServiceAccessKeyHeaderKey, AddressServiceAccessKey);
        httpWebRequest.Headers.Add(AuthenticationUserAccessKey, accessKey);
        httpWebRequest.Host = theUri.Host;

        var serializer = new JavaScriptSerializer();

        string json = serializer.Serialize(addressBook);
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        byte[] bytes = encoding.GetBytes(json);
        httpWebRequest.ContentLength = bytes.Length;

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            streamWriter.Write(json);
        }

        try
        {
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                Account acc = JsonConvert.DeserializeObject<Account>(result);
            }
        }
        catch (Exception e)
        {
            throw new Exception("住所管理サービスの更新処理でエラーが発生しました。[PostReceiversGroups] :" + e);
        }
        return true;
    }

    private Boolean GetReceivers(string accessKey, out AMFGreetoReceiver[] receivers)
    {
        Uri theUri = new Uri(athenaSiteUrl + "/api/receivers");
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(theUri);
        httpWebRequest.KeepAlive = false;
        httpWebRequest.Accept = "application/json";
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "GET";
        httpWebRequest.Headers.Add(AuthenticationServiceCodeHeaderKey, AddressServiceAccessCode);
        httpWebRequest.Headers.Add(uthenticationServiceAccessKeyHeaderKey, AddressServiceAccessKey);
        httpWebRequest.Headers.Add(AuthenticationUserAccessKey, accessKey);
        httpWebRequest.Host = theUri.Host;

        receivers = null;
        try
        {
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                string result = streamReader.ReadToEnd();
                var ser = new JavaScriptSerializer();
                receivers = ser.Deserialize<AMFGreetoReceiver[]>(result);
                streamReader.Close();
                httpResponse.Close();
            }              
        }
        catch (Exception e)
        {
            throw new Exception("Receivers取得処理でエラーが発生しました。[GetReceivers] :" + e);
        }
        return true;
    }

0 个答案:

没有答案