This is my first try with an API and HttpWebRequest Class in Visual Studio 2008 c++. I try to POST some variables to a link and want an link value from the API back, but I get an Response (not from API) : this->t_bbcode->Text = dataStream->ToString(); // System.Net.ConnectStream

I think there is an error in my Request.

/// Request / Bitte
 HttpWebRequest^ myReq = dynamic_cast<HttpWebRequest^>(WebRequest::Create( "http://linksave.in/protect?api=TRUE" ));
 myReq->Timeout = 3000;
 myReq->ServicePoint->Expect100Continue = false;
 myReq->Method = "POST";
 String^ inputData = "protect=true links=";
 ASCIIEncoding^ encoding = gcnew ASCIIEncoding;
 array<Byte>^ byte1 = encoding->GetBytes( inputData );
 // Set the content type of the data being posted.
     myReq->ContentType = "application/x-www-form-urlencoded";
     // Set the content length of the String* being posted.
 myReq->ContentLength = byte1->Length;
 System::IO::Stream^ newStream = myReq->GetRequestStream();
 newStream->Write( byte1, 0, byte1->Length );
 myReq->GetRequestStream();
 Console::WriteLine( "The value of 'ContentLength' property after sending the data is {0}", myReq->ContentLength );


 /// Response / Antwort

    HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(myReq->GetResponse());
System::IO::Stream^ dataStream = response->GetResponseStream();
this->t_bbcode->Text = dataStream->ToString();

// Close the Stream Object*.
  newStream->Close();
response->Close();

Thanks :).

Dani AI

Generated

(and thanks for the namespace tip) — short diagnosis and a focused checklist.

Calling dataStream->ToString() only returns the Stream object's type name; it does not return the HTTP body. Also note a few other issues visible in the sample: the POST body must be form-encoded (name=value pairs separated by &), parameter values must be URL-encoded, the request stream must be closed before calling GetResponse(), and there is an accidental second GetRequestStream() call. A very short timeout (3000 ms) can also abort slow responses.

Fix outline and minimal C++/CLI pattern (replace placeholders as needed):

String^ fileUrl = "<your-file-url>";
String^ post = "protect=true&links=" + System::Uri::EscapeDataString(fileUrl);
array<Byte>^ payload = System::Text::Encoding::UTF8->GetBytes(post);

HttpWebRequest^ req = dynamic_cast<HttpWebRequest^>(WebRequest::Create(apiUri));
req->Method = "POST";
req->ContentType = "application/x-www-form-urlencoded";
req->ContentLength = payload->Length;

// write and close request stream BEFORE GetResponse
System::IO::Stream^ s = req->GetRequestStream();
s->Write(payload, 0, payload->Length);
s->Close();

HttpWebResponse^ resp = dynamic_cast<HttpWebResponse^>(req->GetResponse());
System::IO::StreamReader^ r = gcnew System::IO::StreamReader(resp->GetResponseStream(), System::Text::Encoding::UTF8);
String^ responseText = r->ReadToEnd();
r->Close();
resp->Close();

this->t_bbcode->Text = responseText;

Extra troubleshooting tips: wrap GetResponse() in a try/catch for WebException and read ex->Response to see server error bodies; remove the duplicate GetRequestStream() call; increase Timeout to a higher value while testing; and use a proxy/sniffer like Fiddler to confirm the exact bytes sent (content-type, POST body, headers). The core fixes are: send a correctly encoded form body, close the request stream, then read the response with a StreamReader->ReadToEnd() instead of calling ToString().

Added the followings before your cpp program.

using namespace System;
using namespace System::Net;


:) :-)

Hi thanks for you answer.
I did it before, sorry it's not the whole code.
The Problem is i get from GetResponseStream() the Value System.Net.ConnectStream instead of an answer from the webserver.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.