Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 30, 2012 10:36 AM by maddyrafi8975
Member
134 Points
300 Posts
Apr 30, 2012 09:48 AM|LINK
I have coding like this for webservice its no error when build, but it show object references not set to an instance of that object error , protected void Page_Load(object sender, EventArgse)
{
string strURL = null;
string strResult = null;
HttpWebRequest wbrq = null;
HttpWebResponse wbrs = null;
StreamReader sr = null;
strURL ="http://aspnetlibrary.com/articles.aspx?Page=1";
wbrq = (HttpWebRequest)WebRequest.Create(strURL);
wbrq.Method ="GET";
sr =new StreamReader(wbrs.GetResponseStream());//Error Null error occurs here ?
strResult = sr.ReadToEnd().Trim();
sr.Close();
TextBox1.Text = strResult;
}
public void Default1()
Load += Page_Load;
Contributor
3230 Points
668 Posts
Apr 30, 2012 09:56 AM|LINK
hi,
try this code
string strURL = http://aspnetlibrary.com/articles.aspx?Page=1;
wbrs = (HttpWebResponse)wbrq.GetResponse();
sr = new StreamReader(wbrs.GetResponseStream());
string strResult = sr.ReadToEnd().Trim();
Apr 30, 2012 10:01 AM|LINK
Thank You, Then what i will get output from this code ? kindly tel me pls...
Star
9204 Points
1570 Posts
Apr 30, 2012 10:02 AM|LINK
Your line should be something like
sr = new StreamReader(wbrq.GetResponse().GetResponseStream());
In your code you are accessing wbrs which is the response, yet you haven't retrieved the response yet so that variable is a null reference.
Apr 30, 2012 10:05 AM|LINK
Apr 30, 2012 10:15 AM|LINK
protected void Page_Load(object sender, EventArgs e)
string strURL = "";
string strPostData = "";
string strResult = "";
StreamWriter sw = null;
strURL =http://www.webcom.com/cgi-bin/form;
strPostData =string.Format("your_name={0}&userid={1}&form_name={2}", "Mark Smith", "webcom", "tutortest");
wbrq.Method ="POST";//Same object reference error
wbrq.Referer ="http://www.webcom.com/cgi-bin/form";
wbrq.ContentLength = strPostData.Length;
wbrq.ContentType ="application/x-www-form-urlencoded";
sw =new StreamWriter(wbrq.GetRequestStream());
sw.Write(strPostData);
sr =new StreamReader(wbrs.GetResponseStream());
public Default1()
sw.Close();
Apr 30, 2012 10:23 AM|LINK
i think you are missing this line
before this line
Apr 30, 2012 10:29 AM|LINK
i get output just html this output is correct na ?
Apr 30, 2012 10:32 AM|LINK
Yes, that is what you will get. If you paste the url into your browser then select "View Source", that is basically what your code is doing.
Apr 30, 2012 10:36 AM|LINK
Yes Thank You so much...
maddyrafi897...
Member
134 Points
300 Posts
Object Reference Error
Apr 30, 2012 09:48 AM|LINK
I have coding like this for webservice its no error when build, but it show object references not set to an instance of that object error ,
protected void Page_Load(object sender, EventArgse)
{
string strURL = null;
string strResult = null;
HttpWebRequest wbrq = null;
HttpWebResponse wbrs = null;
StreamReader sr = null;
strURL ="http://aspnetlibrary.com/articles.aspx?Page=1";
wbrq = (HttpWebRequest)WebRequest.Create(strURL);
wbrq.Method ="GET";
sr =new StreamReader(wbrs.GetResponseStream());//Error Null error occurs here ?
strResult = sr.ReadToEnd().Trim();
sr.Close();
TextBox1.Text = strResult;
}
public void Default1()
{
Load += Page_Load;
}
tusharrs
Contributor
3230 Points
668 Posts
Re: Object Reference Error
Apr 30, 2012 09:56 AM|LINK
hi,
try this code
HttpWebRequest wbrq = null;
HttpWebResponse wbrs = null;
StreamReader sr = null;
string strURL = http://aspnetlibrary.com/articles.aspx?Page=1;
wbrq = (HttpWebRequest)WebRequest.Create(strURL);
wbrq.Method ="GET";
wbrs = (HttpWebResponse)wbrq.GetResponse();
sr = new StreamReader(wbrs.GetResponseStream());
string strResult = sr.ReadToEnd().Trim();
sr.Close();
( Mark as Answer if it helps you out )
View my Blog
maddyrafi897...
Member
134 Points
300 Posts
Re: Object Reference Error
Apr 30, 2012 10:01 AM|LINK
Thank You, Then what i will get output from this code ? kindly tel me pls...
AidyF
Star
9204 Points
1570 Posts
Re: Object Reference Error
Apr 30, 2012 10:02 AM|LINK
Your line should be something like
In your code you are accessing wbrs which is the response, yet you haven't retrieved the response yet so that variable is a null reference.
maddyrafi897...
Member
134 Points
300 Posts
Re: Object Reference Error
Apr 30, 2012 10:05 AM|LINK
Thank You, Then what i will get output from this code ? kindly tel me pls...
maddyrafi897...
Member
134 Points
300 Posts
Re: Object Reference Error
Apr 30, 2012 10:15 AM|LINK
protected void Page_Load(object sender, EventArgs e)
{
string strURL = "";
string strPostData = "";
string strResult = "";
HttpWebRequest wbrq = null;
HttpWebResponse wbrs = null;
StreamWriter sw = null;
StreamReader sr = null;
strURL =http://www.webcom.com/cgi-bin/form;
strPostData =string.Format("your_name={0}&userid={1}&form_name={2}", "Mark Smith", "webcom", "tutortest");
wbrq.Method ="POST";//Same object reference error
wbrq.Referer ="http://www.webcom.com/cgi-bin/form";
wbrq.ContentLength = strPostData.Length;
wbrq.ContentType ="application/x-www-form-urlencoded";
sw =new StreamWriter(wbrq.GetRequestStream());
sw.Write(strPostData);
sr =new StreamReader(wbrs.GetResponseStream());
strResult = sr.ReadToEnd().Trim();
TextBox1.Text = strResult;
}
public Default1()
{
Load += Page_Load;
}
sr.Close();
sw.Close();
tusharrs
Contributor
3230 Points
668 Posts
Re: Object Reference Error
Apr 30, 2012 10:23 AM|LINK
hi,
i think you are missing this line
wbrq = (HttpWebRequest)WebRequest.Create(strURL);
before this line
wbrq.Method ="POST";//Same object reference error
( Mark as Answer if it helps you out )
View my Blog
maddyrafi897...
Member
134 Points
300 Posts
Re: Object Reference Error
Apr 30, 2012 10:29 AM|LINK
i get output just html this output is correct na ?
AidyF
Star
9204 Points
1570 Posts
Re: Object Reference Error
Apr 30, 2012 10:32 AM|LINK
Yes, that is what you will get. If you paste the url into your browser then select "View Source", that is basically what your code is doing.
maddyrafi897...
Member
134 Points
300 Posts
Re: Object Reference Error
Apr 30, 2012 10:36 AM|LINK
Yes Thank You so much...