473,785 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ 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.master link.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(postD ata);

myWebClient->Headers->Add(S"Conten t-Type",

S"applicatio n/x-www-form-urlencoded");

//UploadData implicitly sets HTTP POST as the request method.
Byte responseArray[] = myWebClient->UploadData(uri String, 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.master link.com.tw/futures/QuotePrice.jsp" ;
HttpWebRequest *request = dynamic_cast<Ht tpWebRequest*>

(WebRequest::Cr eate(uriString) );
request->Method = "POST";
request->Credentials = CredentialCache ::DefaultCreden tials;
request->ContentType = S"applicatio n/x-www-form-urlencoded";

HttpWebResponse * response = dynamic_cast<Ht tpWebResponse*>
(request->GetResponse()) ;
// Gets the stream associated with the response.
Stream* receiveStream = response->GetResponseStr eam();
Encoding* encode = System::Text::E ncoding::GetEnc oding(S"utf-8");

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

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

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

String* uriString =
S"trade7.master link.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(postD ata);

myWebClient->Headers->Add(S"Conten t-Type",

S"applicatio n/x-www-form-urlencoded");

//UploadData implicitly sets HTTP POST as the request method.
Byte responseArray[] = myWebClient->UploadData(uri String,
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.master link.com.tw/futures/QuotePrice.jsp" ; HttpWebRequest
*request = dynamic_cast<Ht tpWebRequest*>

(WebRequest::Cr eate(uriString) );
request->Method = "POST";
request->Credentials = CredentialCache ::DefaultCreden tials;
request->ContentType = S"applicatio n/x-www-form-urlencoded";

HttpWebResponse * response = dynamic_cast<Ht tpWebResponse*>

(request->GetResponse()) ; // Gets the stream associated with the
response. Stream* receiveStream = response->GetResponseStr eam();
Encoding* encode = System::Text::E ncoding::GetEnc oding(S"utf-8");

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

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


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

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

// C# ;->
byte[] bytes = Encoding.UTF8.G etBytes(data);
HttpWebRequest request = (HttpWebRequest ) WebRequest.Crea te(url);
request.Method = "POST";
request.Content Length = bytes.Length;
request.Content Type = "applicatio n/x-www-form-urlencoded";
using (Stream requestStream = request.GetRequ estStream()) {
requestStream.W rite(bytes, 0, bytes.Length);
}

HttpWebResponse response = (HttpWebRespons e) request.GetResp onse();

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
3225
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 in a variable and make the request again with that cookie as a header value (Digest Authentication). But, the problem we are facing is: That the HttpWebRequest is getting two responses simultaneously, which means that the code should wait after...
3
750
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 : http://xxxxxx/axis-cgi/com/ptz.cgi?pan=100 (GET method) or using POST. To do that I'm using the HttpWebRequest and HttpWebResponse class of
16
12647
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 MSHTML objects to really load the HTML and request all of the images on site? string lcUrl = http://www.cnn.com; // *** Establish the request
1
12613
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 kind of connectivity issue on the machine. It didn't make sense though, because I can open up a browser on the same machine and easily browse the web. I'm stumped. I looked over my code for any errors and just couldn't find what I was doing...
1
2803
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 in a variable and make the request again with that cookie as a header value (Digest Authentication). But, the problem we are facing is: That the HttpWebRequest is getting two responses simultaneously, which means that the code should wait after the...
2
350
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 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",
0
1513
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 ASP content, and then just writes it to the page. In development and testing, there were no problems. Once deployed to production, after the site runs for a few hours, it starts to slow down and throw countless "Operation Has Timed Out"...
0
2039
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 ASP content, and then just writes it to the page. In development and testing, there were no problems. Once deployed to production, after the site runs for a few hours, it starts to slow down and throw countless "Operation Has Timed Out"...
8
3233
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 correct me where am going wrong. string result; System.Net.HttpWebRequest request,request1; System.Net.HttpWebResponse response,response1;
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9957
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
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 we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.