Monday, March 26, 2012

System.Net HttpWebRequest - post a form

(Type your message here)
From: senthil t
There is a third party website that we use=2E Whenever someone=
unsubscribes from our website, some one has to go to that third=
party website, enter the unsubscribed user info and submit the=
form=2E We are doing this manually=2E What I would like is, whenever=
any user unsubscribes from our website, auto post the third=
party website form with all the user information from my =2Enet=
code=2E This is the code I have right now=2E But this is not=
working=2E
HttpWebRequest webRequest =3D (HttpWebRequest) WebRequest=2ECreate=
(URL);
webRequest=2EKeepAlive =3D false;
webRequest=2ETimeout =3D 100000;
webRequest=2EMethod =3D "POST";
webRequest=2EContentType =3D "application/x-www-form-urlencoded";
byte[] requestBytes =3D (new=
System=2EText=2EASCIIEncoding())=2EGetBy
tes (PostData);
webRequest=2EContentLength =3D requestBytes=2ELength;
Stream requestStream =3D webRequest=2EGetRequestStream();
requestStream=2EWrite (requestBytes, 0, requestBytes=2ELength);
requestStream=2EClose();
webResponse =3D (HttpWebResponse) webRequest=2EGetResponse();
sr =3D new StreamReader (webResponse=2EGetResponseStream(),=
System=2EText=2EEncoding=2EASCII);
response =3D sr=2EReadToEnd ();
With =2Easpx page, it was not doing anything until I added the=
viewstate info to the postdata=2E But is throwing an error at this=
line=2E
webResponse =3D (HttpWebResponse) webRequest=2EGetResponse();
Internal Server Error=2ESystem=2ENet=2EHttpWebResponse at=
System=2ENet=2EHttpWebRequest=2ECheckFin
alStatus() at=
System=2ENet=2EHttpWebRequest=2EEndGetRe
sponse(IAsyncResult=
asyncResult) at System=2ENet=2EHttpWebRequest=2EGetRespo
nse()
Can any one tell me what is wrong with what I am doing?
Posted by a user from =2ENET 247 (http://www=2Edotnet247=2Ecom/)
<Id>0ZTNUiUrPECOfo+INVokdg=3D=3D</Id>Hi senthil:
What I've done in the past is use a tool like Fiddler [1] to compare
what my program is POSTing to the server versus what happens when I
use a browser.
http://www.fiddlertool.com/fiddler/
Scott
http://www.OdeToCode.com/blogs/scott/
On Sun, 15 May 2005 05:03:55 -0700, senthil t via .NET 247
<anonymous@.dotnet247.com> wrote:

>(Type your message here)
>--
>From: senthil t
>There is a third party website that we use. Whenever someone unsubscribes from our
website, some one has to go to that third party website, enter the unsubscribed user
info and submit the form. We are doing this manually. What I would like is, wheneve
r a
ny user unsubscribes from our website, auto post the third party website form with all the
user information from my .net code. This is the code I have right now. But this is not work
ing.
> HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create (URL);
> webRequest.KeepAlive = false;
> webRequest.Timeout = 100000;
> webRequest.Method = "POST";
> webRequest.ContentType = "application/x-www-form-urlencoded";
> byte[] requestBytes = (new System.Text.ASCIIEncoding()).GetBytes (PostDa
ta);
> webRequest.ContentLength = requestBytes.Length;
> Stream requestStream = webRequest.GetRequestStream();
> requestStream.Write (requestBytes, 0, requestBytes.Length);
> requestStream.Close();
> webResponse = (HttpWebResponse) webRequest.GetResponse();
> sr = new StreamReader (webResponse.GetResponseStream(), System.Text.Enco
ding.ASCII);
> response = sr.ReadToEnd ();
>With .aspx page, it was not doing anything until I added the viewstate info
to the postdata. But is throwing an error at this line.
>webResponse = (HttpWebResponse) webRequest.GetResponse();
>Internal Server Error.System.Net.HttpWebResponse at System.Net.HttpWebReque
st.CheckFinalStatus() at System.Net.HttpWebRequest.EndGetResponse(IAsyncResu
lt asyncResult) at System.Net.HttpWebRequest.GetResponse()
>Can any one tell me what is wrong with what I am doing?
>--
>Posted by a user from .NET 247 (http://www.dotnet247.com/)
><Id>0ZTNUiUrPECOfo+INVokdg==</Id>

0 comments:

Post a Comment