Пример #1
0
        private void GetResponseCallback(IAsyncResult asyncResult)
        {
            HttpWebResponse response = null;

            try
            {
                response = m_webRequest.EndGetResponse(asyncResult) as HttpWebResponse;
            }
            catch (WebException e)
            {
                LOG.ERROR("EndGetResponse", e);
                response = (HttpWebResponse)e.Response;
                m_code   = Convert.ToInt32(response.StatusCode);
            }

            Stream stream = response.GetResponseStream();

            LOG.INFO("openInputStream done");

            CookieContainer container = new CookieContainer();

            container.Add(new Uri(m_strUrl), response.Cookies);
            m_strCookies = container.GetCookieHeader(new Uri(m_strUrl));

            m_code = Convert.ToInt32(response.StatusCode);
            LOG.INFO("getResponseCode : " + m_code);

            readHeaders(m_headers);
            copyHashtable(m_OutHeaders, m_headers);

            try
            {
                if (m_code >= 400)
                {
                    LOG.ERROR("Error retrieving data: " + m_code);
                    if (m_code == Convert.ToInt32(HttpStatusCode.Unauthorized) && m_oSession != null)
                    {
                        LOG.ERROR("Unauthorize error.Client will be logged out");
                        m_oSession.logout();
                    }

                    m_strRespBody = readFully(stream, getResponseEncoding());
                    LOG.TRACE("Response body: " + m_strRespBody);
                }
                else
                {
                    long len = response.ContentLength;
                    LOG.INFO("fetchRemoteData data size:" + len);

                    m_strRespBody = readFully(stream, getResponseEncoding());
                    LOG.INFO("fetchRemoteData data readFully.");
                }
            }
            finally
            {
                stream.Close();
                response.Close();
                m_respWaitEvent.Set();
            }
        }
Пример #2
0
        private void GetResponseCallback(IAsyncResult asyncResult)
        {
            HttpWebResponse response = null;

            try
            {
                response = m_webRequest.EndGetResponse(asyncResult) as HttpWebResponse;
            }
            catch (WebException e)
            {
                if (!m_bCancel)
                {
                    LOG.ERROR("EndGetResponse", e);
                    response = (HttpWebResponse)e.Response;
                    m_code   = response != null?Convert.ToInt32(response.StatusCode) : 0;
                }
                else
                {
                    LOG.INFO("Request was cancelled by user.");
                }
            }
            catch (ArgumentNullException e)
            {
                m_code = 1;
                m_respWaitEvent.Set();
                return;
            }
            catch (Exception e)
            {
                ///LOG.ERROR("EndGetResponse", e);
                m_respWaitEvent.Set();
                return;
            }

            if (response == null || m_bCancel)
            {
                m_respWaitEvent.Set();
                return;
            }

            Stream stream = response.GetResponseStream();

            m_code = Convert.ToInt32(response.StatusCode);
            LOG.INFO("getResponseCode : " + m_code);

            m_strCookies = makeClientCookie(response);

            readHeaders(m_headers, response);
            copyHashtable(m_OutHeaders, m_headers);

            try
            {
                if (m_code >= 400)
                {
                    LOG.ERROR("Error retrieving data: " + m_code);
                    if (m_code == Convert.ToInt32(HttpStatusCode.Unauthorized) && m_oSession != null)
                    {
                        LOG.ERROR("Unauthorize error.Client will be logged out");
                        m_oSession.logout();
                    }

                    m_strRespBody = readFully(stream, getResponseEncoding());
                    LOG.TRACE("Response body: " + m_strRespBody);
                }
                else
                {
                    long len = response.ContentLength;
                    LOG.INFO("fetchRemoteData data size:" + len);

                    m_strRespBody = readFully(stream, getResponseEncoding());
                    LOG.INFO("fetchRemoteData data readFully.");
                }
            }
            finally
            {
                stream.Close();
                response.Close();
                m_respWaitEvent.Set();
            }
        }
Пример #3
0
        void doSyncClientChanges()
        {
            String[]  arUpdateTypes = { "create", "update", "delete" };
            boolean[] arUpdateSent  = { false, false, false };

            m_arMultipartItems.removeAllElements();
            m_arBlobAttrs.removeAllElements();
            String  strBody = "{\"source_name\":" + JSONEntry.quoteValue(getName()) + ",\"client_id\":" + JSONEntry.quoteValue(getSync().getClientID());
            boolean bSend   = false;
            int     i       = 0;

            for (i = 0; i < 3 && getSync().isContinueSync(); i++)
            {
                String strBody1;
                strBody1 = makePushBody_Ver3(arUpdateTypes[i], true);
                if (strBody1.length() > 0)
                {
                    strBody += "," + strBody1;

                    String strBlobAttrs = "";
                    for (int j = 0; j < (int)m_arBlobAttrs.size(); j++)
                    {
                        if (strBlobAttrs.length() > 0)
                        {
                            strBlobAttrs += ",";
                        }

                        strBlobAttrs += JSONEntry.quoteValue((String)m_arBlobAttrs.elementAt(j));
                    }

                    if (strBlobAttrs.length() > 0)
                    {
                        strBody += ",\"blob_fields\":[" + strBlobAttrs + "]";
                    }

                    arUpdateSent[i] = true;
                    bSend           = true;
                }
            }
            strBody += "}";

            if (bSend)
            {
                LOG.INFO("Push client changes to server. Source: " + getName() + "Size :" + strBody.length());
                LOG.TRACE("Push body: " + strBody);

                try{
                    if (m_arMultipartItems.size() > 0)
                    {
                        NetRequest.MultipartItem oItem = new NetRequest.MultipartItem();
                        oItem.m_strBody = strBody;
                        //oItem.m_strContentType = getProtocol().getContentType();
                        oItem.m_strName = "cud";
                        m_arMultipartItems.addElement(oItem);

                        NetResponse resp = getNet().pushMultipartData(getProtocol().getClientChangesUrl(), m_arMultipartItems, getSync(), null);
                        if (!resp.isOK())
                        {
                            getSync().setState(SyncEngine.esStop);
                            m_nErrCode = RhoAppAdapter.ERR_REMOTESERVER;
                            m_strError = resp.getCharData();
                        }
                    }
                    else
                    {
                        NetResponse resp = getNet().pushData(getProtocol().getClientChangesUrl(), strBody, getSync());
                        if (!resp.isOK())
                        {
                            getSync().setState(SyncEngine.esStop);
                            m_nErrCode = RhoAppAdapter.ERR_REMOTESERVER;
                            m_strError = resp.getCharData();
                        }
                    }
                }catch (Exception exc)
                {
                    m_nErrCode = RhoAppAdapter.getNetErrorCode(exc);
                    throw exc;
                }
            }

            for (i = 0; i < 3 && getSync().isContinueSync(); i++)
            {
                if (arUpdateSent[i])
                {
                    //oo conflicts
                    if (i < 1)       //create
                    {
                        getDB().executeSQL("UPDATE changed_values SET sent=2 WHERE source_id=? and update_type=? and sent=1", getID(), arUpdateTypes[i]);
                    }
                    else
                    {
                        //
                        getDB().executeSQL("DELETE FROM changed_values WHERE source_id=? and update_type=? and sent=1", getID(), arUpdateTypes[i]);
                    }
                }
            }

            m_arMultipartItems.removeAllElements();
            m_arBlobAttrs.removeAllElements();
        }