We help IT Professionals succeed at work.

WebRequest.Create

sman003
sman003 asked
on
283 Views
Last Modified: 2010-04-16
I want to do a GET not a POST to the url "http://localhost/Demo/default.asp?restart" i need the partial querysting "?restart" to be there. Current code I have does a post:

==================

string sReturned = null;
sStartDate = System.DateTime.Now;
string postData = "?restart";
string webWhere = "http://localhost/Demo/default.asp"

byte[] sSend = Encoding.ASCII.GetBytes(postData);
int sLength = sSend.Length;
WebRequest myRequest = WebRequest.Create(webWhere);
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.Method = "POST";
myRequest.ContentLength = sLength;
Stream sStream = myRequest.GetRequestStream();
sStream.Write(sSend, 0, sLength);
sStream.Close();

WebResponse myResponse = myRequest.GetResponse();
sStream = myResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader rStream = new StreamReader(sStream, encode);
Char[] read = new Char[256];
int count = rStream.Read(read, 0, 256);

while (count > 0)
{
      String str = new String(read, 0, count);
      sReturned = sReturned + str;
      count = rStream.Read(read, 0, 256);
}

//sReturned now = html context of page

========================

If I change myRequest.Method = "GET" or remove that line setting method = "POST" it fails.
Comment
Watch Question

Commented:
You can only use the ContentType, ContentLength, and GetRequestStream() call when doing a POST. A GET does only support getting a resource (without sending any data), while a POST allows you to also send along some data. See HTTP specs for more info.

Commented:
Oh, and use
string webWhere = "http://localhost/Demo/default.asp?restart";
to specify the parameters for the GET.

Author

Commented:
I've tried setting string webWhere = "http://localhost/Demo/default.asp?restart"; and it fails.

Doing this:

============================================
WebRequest myRequest = WebRequest.Create("http://localhost/Demo/default.asp?restart");
Stream sStream = myRequest.GetRequestStream();
sStream.Close();

WebResponse myResponse = myRequest.GetResponse();
sStream = myResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader rStream = new StreamReader(sStream, encode);
Char[] read = new Char[256];
int count = rStream.Read(read, 0, 256);

while (count > 0)
{
     String str = new String(read, 0, count);
     sReturned = sReturned + str;
     count = rStream.Read(read, 0, 256);
}

//sReturned now = html context of page
==================================

It always gets stuck/times out.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
Sorry. Missed one thing...

[...]
    using (StreamReader rStream = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8)) {
[...]
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.