GetRequestStream hangs

R

ratnakarp

Hi,

I'm new to httpwebrequest programming. I'm using httpwebrequest to
pull the rss file with contenttype as "GET". it works fine. I get all
the results and store all the values in the list. the list has a url
(desclink) property too. i do httpwebrequest again for these url's
with contenttype as "POST". some how i dont understand the problem,
but my page hangs and doesnt retreive any value. i'm pasting the code
below, any help would be appreciated.


Default.aspx
---------------------

XmlParse test1 = new XmlParse();// see the code below for
xmlparse.cs

List<XmlTags> tags1 = new List<XmlTags>();
// System.Net.WebProxy objProxy = new
System.Net.WebProxy("http://usushproxy01:80");
tags1= test1.GetXmlInfo("<rss file"> );
HttpWebRequest objRequest;
for (int i = 1; i < tags1.Count; i++)
{
// Response.Write(tags1.Description + "<br>");
string strPost = "Hello";
//System.Net.WebProxy objProxy1 = new
System.Net.WebProxy("http://usushproxy01:80");
objRequest =
(HttpWebRequest)WebRequest.Create(tags1.DescLink );

//objRequest.Proxy = objProxy;
objRequest.Method = "POST";
objRequest.ContentType = "application/x-www-form-
urlencoded";
objRequest.ContentLength = strPost.Length;
//objRequest.Timeout = 1000000;
//objRequest.KeepAlive = false;

StreamWriter myWriter = new
StreamWriter(objRequest.GetRequestStream());
myWriter.Flush();
myWriter.Write(strPost);


myWriter.Close();

objRequest.GetRequestStream().Close();

string strHold;
//HtmlMetaParser.
objRequest.GetRequestStream().Close();
HttpWebResponse objResponse =
(HttpWebResponse)objRequest.GetResponse();

using (StreamReader sr = new
StreamReader(objResponse.GetResponseStream()))
{
// sr something
strHold = sr.ReadToEnd().ToString();
List<HtmlMeta> Meta1 = HtmlMetaParser.Parse(strHold);
string strmeta="";
for (int j = 0; j < Meta1.Count; j++)
{
strmeta = Meta1[j].Content.ToString();
}
Response.Write(strmeta + "<br><br><br><br><hr/>");
sr.Close();
}
objRequest.Abort();
}



XmlParse.cs
--------------------------
public class XmlParse
{
public XmlParse()
{
//
// TODO: Add constructor logic here
//
}
public List<XmlTags> GetXmlInfo(string uri)
{
List<XmlTags> myList = new List<XmlTags>();


HttpWebRequest objRequest =
(HttpWebRequest)WebRequest.Create("http://nbcumv.com/feeds/
bravo.en.xml");
string strPost = "Hello";
//objRequest.Proxy = objProxy;
objRequest.Method = "GET";
objRequest.ContentType = "application/x-www-form-urlencoded";
objRequest.ContentLength = strPost.Length;
//objRequest.Timeout = 1000000;
//objRequest.KeepAlive = false;
HttpWebResponse objResponse =
(HttpWebResponse)objRequest.GetResponse();


string str;
string strHold;
using (StreamReader sr = new
StreamReader(objResponse.GetResponseStream()))
{
strHold = sr.ReadToEnd().ToString();
sr.Close();
}
objRequest.Abort();

XmlDocument doc = new XmlDocument();
doc.LoadXml(strHold );

XmlNodeList nodelist = doc.GetElementsByTagName("item");

for (int i = 0; i < nodelist.Count; i++)
{
XmlTags XmlTags1 = new XmlTags();

XmlTags1.Title = nodelist.Item(i).ChildNodes[0].InnerText;
XmlTags1.Link = nodelist.Item(i).ChildNodes[1].InnerText;

XmlTags1.Description =
nodelist.Item(i).ChildNodes[2].InnerText;
string strtest =
XmlTags1.Description.Substring(XmlTags1.Description.IndexOf("</a>"));
XmlTags1.Description = strtest.Replace("</a>","");
string str2 = nodelist.Item(i).ChildNodes[2].InnerText;
XmlTags1.DescLink =
nodelist.Item(i).ChildNodes[2].InnerText.Substring(9,
nodelist.Item(i).ChildNodes[2].InnerText.IndexOf("><img src=")-10);
XmlTags1.ImageLink =
nodelist.Item(i).ChildNodes[2].InnerText.Substring(nodelist.Item(i).ChildNodes[2].InnerText.IndexOf("><img
src=") + 11, str2.LastIndexOf("rss=1") -
nodelist.Item(i).ChildNodes[2].InnerText.IndexOf("><img src=") - 6);

XmlTags1.Author =
nodelist.Item(i).ChildNodes[3].InnerText;
XmlTags1.Date = nodelist.Item(i).ChildNodes[4].InnerText;
myList.Add(XmlTags1);
}
objResponse.Close();
return myList;
}
}
 
J

Joerg Jooss

Thus wrote (e-mail address removed),
Hi,

I'm new to httpwebrequest programming. I'm using httpwebrequest to
pull the rss file with contenttype as "GET". it works fine. I get all
the results and store all the values in the list. the list has a url
(desclink) property too. i do httpwebrequest again for these url's
with contenttype as "POST". some how i dont understand the problem,
but my page hangs and doesnt retreive any value. i'm pasting the code
below, any help would be appreciated.

There are a few potential bugs in your code (see below), and I don't think
that POST is the right thing to do here anyway.

But for your purpose, you're better off using WebClient.UploadValues() if
you really need to POST.
Default.aspx
---------------------
XmlParse test1 = new XmlParse();// see the code below for
xmlparse.cs

List<XmlTags> tags1 = new List<XmlTags>();
// System.Net.WebProxy objProxy = new
System.Net.WebProxy("http://usushproxy01:80");
tags1= test1.GetXmlInfo("<rss file"> );
HttpWebRequest objRequest;
for (int i = 1; i < tags1.Count; i++)
{
// Response.Write(tags1.Description + "<br>");
string strPost = "Hello";
//System.Net.WebProxy objProxy1 = new
System.Net.WebProxy("http://usushproxy01:80");
objRequest =
(HttpWebRequest)WebRequest.Create(tags1.DescLink );
//objRequest.Proxy = objProxy;
objRequest.Method = "POST";
objRequest.ContentType = "application/x-www-form-
urlencoded";
objRequest.ContentLength = strPost.Length;


That's dangerous. You assume that your string length equals the post size,
but that may or may not be true depending on the character encoding. And
both headers don't apply in the GET case.
//objRequest.Timeout = 1000000;
//objRequest.KeepAlive = false;
StreamWriter myWriter = new
StreamWriter(objRequest.GetRequestStream());
myWriter.Flush();

Why's that?
myWriter.Write(strPost);

Note that you should URL encode POST data.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,836
Messages
2,569,750
Members
45,545
Latest member
rapter____0

Latest Threads

Top