[HD2 Windows Phone 7 Q&A, Help & Troubleshooting an] Ashish Goral:
Anybody Pls help me with this I am making a http post request to a server ,
on doing the 1st request i'm able to get the response & i'm able to get the
session id that is been used for further polling & used i the second request as
the cookie value.But on making the second request i'm getting the NullReference
Expection in the response , When i am monitoring it on a Packet capturing
software i find that i'm getting the required data in the captured packets but
unable to read it in the response of the second request in the application code
& i'm getting a NullReferenceExpection

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Text;
using System.Windows.Threading;

namespace MunduNetworkingHttp
{
public partial class MainPage : PhoneApplicationPage
{
private HttpWebRequest UniversalRequest;
private HttpWebRequest request;
private HttpWebResponse response;
private Stream stream;

// Constructor
public MainPage()
{
InitializeComponent();
}

public static string res1;
public static string res2;
public static string sessionid;

public static string valueOfSession;

//Here the Server Link is Specified.
string Server = "server link here";

private void button1_Click(object sender, RoutedEventArgs e)
{

//Here we are callinh the method of the first request.
first_Request();
}

public void first_Request()
{
if (null == UniversalRequest)
{

UniversalRequest = (HttpWebRequest)WebRequest.Create(Server);
UniversalRequest.CookieContainer = new CookieContainer();
UniversalRequest.CookieContainer.Add(new Uri(Server),new Cookie("SESSION","0"));
UniversalRequest.Accept = "*/*";
UniversalRequest.Method = "POST";

}

UniversalRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback1), UniversalRequest);

}
private void GetRequestStreamCallback1(IAsyncResult asynchronousReault1)
{

string XMl_REQUEST1 = "<SAMPLE><Xml string to be sent to server on 1st request'/></SAMPLE>";
request = (HttpWebRequest)asynchronousReault1.AsyncState;
stream= request.EndGetRequestStream(asynchronousReault1);
byte[] postBytes1 = Encoding.UTF8.GetBytes(XMl_REQUEST1);
stream.Write(postBytes1, 0, postBytes1.Length);
stream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback1), request);

}
private void GetResponseCallback1(IAsyncResult asynchronousResult1)
{

request = (HttpWebRequest)asynchronousResult1.AsyncState;
response = (HttpWebResponse)request.EndGetResponse(asynchronousResult1);
stream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(stream);
string responseString1 = streamReader.ReadToEnd();
stream.Close();
streamReader.Close();

//Retreving the value of the session Id.

valueOfSession = (retrive the value from responseString1);

response.Close();
request.Abort();

second_Request();
}

public void second_Request()
{

UniversalRequest = (HttpWebRequest)WebRequest.Create(Server);
UniversalRequest.CookieContainer = new CookieContainer();
UniversalRequest.CookieContainer.Add(new Uri(Server), new Cookie("SESSTOOLBARMUNDU", valueOfSession));
UniversalRequest.ContentType = "application/xml";
UniversalRequest.Method = "POST";
UniversalRequest.Accept = "*/*";

UniversalRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback2), UniversalRequest);

}
private void GetRequestStreamCallback2(IAsyncResult asynchronousResult2)
{

string XMLREQUEST2 = "<SAMPLE2><Xml string data to be sent to the server on 2nd request to the server /></SAMPLE2>";
request = (HttpWebRequest)asynchronousResult2.AsyncState;
stream = request.EndGetRequestStream(asynchronousResult2);
byte[] postBytes2 = Encoding.UTF8.GetBytes(XMLREQUEST2);
stream.Write(postBytes2, 0, postBytes2.Length);
stream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback2), request);

}

private void GetResponseCallback2(IAsyncResult asynchronousResult2)
{

request = (HttpWebRequest)asynchronousResult2.AsyncState;
response = (HttpWebResponse)request.EndGetResponse(asynchronousResult2);// here is where i get the NullReferenceExpection
stream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(stream);
string responseString2 = streamReader.ReadToEnd();
stream.Close();
streamReader.Close();
response.Close();

}

}
}