By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

Post Request and C#

P: n/a
Ori
Hi,

I need to implement the following.

I need to perform a web request (only post methods), but I also need
to send some parameters values with this request.

Can I do it using the WebRequest class (using "POST" method only)?

In the same time I have another question. I need to perform this
request to a File.exe file (the URL which I'm doing this request to
has a fileName.exe in the end of the url). Can i pass a parameters to
the .exe file using "GET" method or post is the only method which I
can use in this case.

Thanks,

Ori.
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> Can I do it using the WebRequest class (using "POST" method only)?

HttpWebRequest HttpWReq =
(HttpWebRequest)WebRequest.Create(
"http:\\www.blabla.com\services\service1.asp"
);

ASCIIEncoding encoding=new ASCIIEncoding();
string postData="username=foo";
postData += ( "&password=boo");
byte[] data = encoding.GetBytes(postData);

HttpWReq.Method = "POST";
HttpWReq.ContentType = "application/x-www-form-urlencoded";
HttpWReq.ContentLength = data.Length;

Stream newStream = HttpWReq.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close();

Luca Paganelli
Nov 15 '05 #2

P: n/a
Perhaps you are interested also looking at the WebClient Class

If you have to "post" some values, like a form submit, it has a very nice
method:

WebClient.UploadValues(...);

"Ori" <or*******@hotmail.com> schrieb im Newsbeitrag
news:b4**************************@posting.google.c om...
Hi,

I need to implement the following.

I need to perform a web request (only post methods), but I also need
to send some parameters values with this request.

Can I do it using the WebRequest class (using "POST" method only)?

In the same time I have another question. I need to perform this
request to a File.exe file (the URL which I'm doing this request to
has a fileName.exe in the end of the url). Can i pass a parameters to
the .exe file using "GET" method or post is the only method which I
can use in this case.

Thanks,

Ori.

Nov 15 '05 #3

P: n/a


do you some examples for WebClient.UploadValues(...); ?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4

P: n/a
Example from:
http://msdn.microsoft.com/library/de...aluestopic.asp

[C#]
Console.Write("\nPlease enter the URL to post data to : ");
string uriString = Console.ReadLine();

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

// Create a new NameValueCollection instance to hold some custom parameters
to be posted to the URL.
NameValueCollection myNameValueCollection = new NameValueCollection();

Console.WriteLine("Please enter the following parameters to be posted to the
URI");
Console.Write("Name:");
string name = Console.ReadLine();

Console.Write("Age:");
string age = Console.ReadLine();

Console.Write("Address:");
string address = Console.ReadLine();

// Add necessary parameter/value pairs to the name/value container.
myNameValueCollection.Add("Name",name);
myNameValueCollection.Add("Address",address);
myNameValueCollection.Add("Age",age);
Console.WriteLine("\nUploading to {0} ...", uriString);

// Upload the NameValueCollection.
byte[] responseArray =
myWebClient.UploadValues(uriString,"POST",myNameVa lueCollection);

// Decode and display the response.
Console.WriteLine("\nResponse received was
:\n{0}",Encoding.ASCII.GetString(responseArray));

"Ori Anavim" <an********@hotmail.com> schrieb im Newsbeitrag
news:OA****************@TK2MSFTNGP11.phx.gbl...


do you some examples for WebClient.UploadValues(...); ?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #5

P: n/a
Well, to put my few lines into that topic. I suppose, that a bigger problem is to find out how to really READ the values in the CGI application than how to send them.

There are plenty of samples how to send GET or POST requests. But how to get them in CGI exe application written in .Net.

Let me present a sample code how to handle the sent values with a GET method to a CGI C# application in a sample console application:

using System;

namespace CGITest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string queryString = Environment.GetEnvironmentVariable("QUERY_STRING") ;

Console.WriteLine("Content-Type:text/html\n");

Console.WriteLine ("QUERY_STRING: " + queryString);

}
}
}

I didn't manage to find any samples or sort out how to read the values sent with a POST method. Will keep on searching. If anyone knows, just let us know.

--
Cezary Nolewajka
mailto:c.*********************@no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply

"Ori" <or*******@hotmail.com> wrote in message news:b4**************************@posting.google.c om...
Hi,

I need to implement the following.

I need to perform a web request (only post methods), but I also need
to send some parameters values with this request.

Can I do it using the WebRequest class (using "POST" method only)?

In the same time I have another question. I need to perform this
request to a File.exe file (the URL which I'm doing this request to
has a fileName.exe in the end of the url). Can i pass a parameters to
the .exe file using "GET" method or post is the only method which I
can use in this case.

Thanks,

Ori.

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.