views:

716

answers:

0

Hi, i hav developed an addin for outlook using c#. i want 2 read a selected email from outlook and post it to a forum using the url. In the following code i m doing the required task but in response string i m getting "isSuccess" as false. And in server (to which i send a request) i m receiving request with sessionUid : 0 but i m passing it as 1. Here i m setting the jsession id of request to id which i got during login... i m not getting wat could b the problem?????

public void Post_email(string Msg, string Subject)
    {
        string strSrvRes = "";
        bool result = false;

        try
        {
            System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();

            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("url");

            //Our method is post, otherwise the buffer (postvars) would be useless
            WebReq.Timeout = 5000;
            WebReq.Method = "POST";
            CookieContainer CC = new CookieContainer();
            WebReq.CookieContainer = CC;

            string cookieHeader = "JSESSIONID=" + sessionid;
            CC.SetCookies(WebReq.RequestUri, cookieHeader);

            int Suid = 1;
            string jsonstr = "{\"topicDraftSubmitRequest\":{\"topicDraft\":{\"categoryName\":\"PebbleTalk Email Service\",\"title\":\""+ Subject +"\",\"categoryId\":23,\"topicId\":0,\"emailIdList\":{\"emailId\":[{\"emailId\":\"[email protected]\"}]},\"descr\":\"" + Msg + "\",\"isInviteEnabled\":false},\"isSuccess\":true,\"sessionUid\":"+ Suid +",\"errorString\":\"\",\"errorId\":0},\"actionId\":117}";

            string parameter = "formType=jsonForm&jsonRequest=" + jsonstr + "&"+ cookieHeader + "&titleId:0&categoryListId:0&descrId:"+ Msg +"&senttoOptions:&senttoValidationId:";

            //titleId, categoryListId, descrId, senttoOptions , senttoValidationId 


            byte[] buffer = Encoding.UTF8.GetBytes(parameter);

            //We use form contentType, for the postvars.
            WebReq.ContentType = "application/x-www-form-urlencoded";

            //The length of the buffer (postvars) is used as contentlength.
            WebReq.ContentLength = buffer.Length;

            //We open a stream for writing the postvars
            Stream PostData = WebReq.GetRequestStream();
            //Now we write, and afterwards, we close. Closing is always important!
            PostData.Write(buffer, 0, buffer.Length);

            PostData.Close();

            //Get the response handle, we have no true response yet!
            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();




            //Let's show some information about the response
            //*********************************************************************
            using (StreamReader sr = new StreamReader(WebResp.GetResponseStream()))
            {

                strSrvRes = sr.ReadToEnd();
                sr.Close();
            }


            WebResp.Close();

            // Filter session ID after resiving from server 

            JsonTextReader reader = new JsonTextReader(new StringReader(strSrvRes));

            while (reader.Read())
            {
                if (reader.Text == "isSuccess")
                {
                    lblError.Visible = true;
                    reader.Read();
                    string strResult = "";
                    strResult = reader.Text;

                    if (strResult == "true")
                    {
                        result = true;
                        lblError.Text = "Posted Successfully";
                        result = true;
                    }
                    else
                    {
                        result = false;
                        lblError.Text = "Post Failed";
                    }

                }
            }

            if (!result)
            {
                MessageBox.Show("Post Failed");
            }
            else
            {

            }

        }
        catch (Exception ex)
        {
           MessageBox.Show(ex.ToString());
        }
    }