473,770 Members | 1,899 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 11567
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
3224
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
12611
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
2797
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
9453
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
10254
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...
1
10036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
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
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2849
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.