473,467 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to use HttpWebRequest and get response back?

I have a url, I pass it to Webclient, and I get response without any problem.

String* uriString = S"trade7.masterlink.com.tw/futures/QuotePrice.jsp";
String* postData = S"";
// Create a new WebClient instance.
WebClient* myWebClient = new WebClient();
// Apply ASCII Encoding to obtain the String* as a Byte array.
Byte postArray[]= Encoding::ASCII->GetBytes(postData);

myWebClient->Headers->Add(S"Content-Type",

S"application/x-www-form-urlencoded");

//UploadData implicitly sets HTTP POST as the request method.
Byte responseArray[] = myWebClient->UploadData(uriString, postArray);
Now I pass the same string to HttpWebRequest. However, I do not get any
response
and get time-out exception. What is wrong with my HttpWebRequest call?

String* uriString = S"trade7.masterlink.com.tw/futures/QuotePrice.jsp";
HttpWebRequest *request = dynamic_cast<HttpWebRequest*>

(WebRequest::Create(uriString));
request->Method = "POST";
request->Credentials = CredentialCache::DefaultCredentials;
request->ContentType = S"application/x-www-form-urlencoded";

HttpWebResponse* response = dynamic_cast<HttpWebResponse*>
(request->GetResponse());
// Gets the stream associated with the response.
Stream* receiveStream = response->GetResponseStream();
Encoding* encode = System::Text::Encoding::GetEncoding(S"utf-8");

// Pipes the stream to a higher level stream reader with the required
encoding format.
StreamReader* readStream = new StreamReader(receiveStream, encode);

String* responseText = readStream->ReadToEnd();
response->Close();

Jul 21 '05 #1
2 11522
Kueishiong Tu wrote:
I have a url, I pass it to Webclient, and I get response without any
problem.

String* uriString =
S"trade7.masterlink.com.tw/futures/QuotePrice.jsp"; String*
postData = S""; // Create a new WebClient instance.
WebClient* myWebClient = new WebClient();
// Apply ASCII Encoding to obtain the String* as a Byte array.
Byte postArray[]= Encoding::ASCII->GetBytes(postData);

myWebClient->Headers->Add(S"Content-Type",

S"application/x-www-form-urlencoded");

//UploadData implicitly sets HTTP POST as the request method.
Byte responseArray[] = myWebClient->UploadData(uriString,
postArray);
Now I pass the same string to HttpWebRequest. However, I do not get
any response
and get time-out exception. What is wrong with my HttpWebRequest
call?

String* uriString =
S"trade7.masterlink.com.tw/futures/QuotePrice.jsp"; HttpWebRequest
*request = dynamic_cast<HttpWebRequest*>

(WebRequest::Create(uriString));
request->Method = "POST";
request->Credentials = CredentialCache::DefaultCredentials;
request->ContentType = S"application/x-www-form-urlencoded";

HttpWebResponse* response = dynamic_cast<HttpWebResponse*>

(request->GetResponse()); // Gets the stream associated with the
response. Stream* receiveStream = response->GetResponseStream();
Encoding* encode = System::Text::Encoding::GetEncoding(S"utf-8");

// Pipes the stream to a higher level stream reader with the
required encoding format.
StreamReader* readStream = new StreamReader(receiveStream, encode);

String* responseText = readStream->ReadToEnd();
response->Close();


You're not posting anything. You're HttpWebRequest based code does not
emulate the line
myWebClient->UploadData(uriString, postArray);

You need to write your post data to the request stream before getting the
response:

// C# ;->
byte[] bytes = Encoding.UTF8.GetBytes(data);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "application/x-www-form-urlencoded";
using (Stream requestStream = request.GetRequestStream()) {
requestStream.Write(bytes, 0, bytes.Length);
}

HttpWebResponse response = (HttpWebResponse) request.GetResponse();

Cheers,

--
Joerg Jooss
jo*********@gmx.net
Jul 21 '05 #2
You are absolutely right. Thank you very much for the help.

Kueishiong Tu

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Satinderpal Singh | last post by:
Hi everyone, We are using HttpWebRequest to create a request to a URI, which requires us to login first. In order to process all the transactions, first we have to login and get the cookie value...
3
by: superseed | last post by:
Hi, I'm currently coding in C# a class to control a PTZ (Pan Tilt Zoom) Camera. To control the camera I have to send request on a CGI on it. Something like this : ...
16
by: thomas peter | last post by:
I am building a precache engine... one that request over 100 pages on an remote server to cache them remotely... can i use the HttpWebRequest and WebResponse classes for this? or must i use the...
1
by: Jeff B | last post by:
I'm trying to create a simple screen scraping application and I kept getting a System.Net.WebException thrown back with a message of "The operation has timed-out." At first I thought it was some...
1
by: Satinderpal Singh | last post by:
Hi everyone, We are using HttpWebRequest to create a request to a URI, which requires us to login first. In order to process all the transactions, first we have to login and get the cookie value...
2
by: Kueishiong Tu | last post by:
I have a url, I pass it to Webclient, and I get response without any problem. String* uriString = S"trade7.masterlink.com.tw/futures/QuotePrice.jsp"; String* postData = S""; // Create a new...
0
by: Alex Papadimoulis | last post by:
Hey Group, I'm in the process of converting an ASP-based site to an ASP.NET site and built a control that wraps around an ASP page. The control simply does a GET to the same server to render the...
0
by: Alex Papadimoulis | last post by:
Hey Group, I'm in the process of converting an ASP-based site to an ASP.NET site and built a control that wraps around an ASP page. The control simply does a GET to the same server to render the...
8
by: shankararaman.s | last post by:
Hi, I am trying to develop an interface which will fetch all my Yahoo mails. I am not able to sign in to yahoo by posting the form with my username & password. Please find my code below and...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.