How to use HttpWebRequest and get response back?

G

Guest

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();
 
T

Tomas Restrepo \(MVP\)

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();

It seems you're passing credentials in the WebRequest case, but not in the
WebClient case... might that be an issue?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top