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

Joerg Jooss

Kueishiong said:
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,
 

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

Top