1
Tech Writer 2.2k 1.6m 22y Hi,
This works for me:
public String HttpPost(String url, String payload)
{
WebResponse result = null;
String strRetVal = "";
try
{
WebRequest req = WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = {'?', '=', '&'};
byte[] SomeBytes = null;
if (payload != null)
{
int i = 0, j;
while( i < payload.Length )
{
j = payload.IndexOfAny( reserved, i );
if ( j == -1 )
{
UrlEncoded.Append( HttpUtility.UrlEncode( payload.Substring( i, payload.Length-i ) ) );
break;
}
UrlEncoded.Append( HttpUtility.UrlEncode( payload.Substring( i, j-i ) ) );
UrlEncoded.Append( payload.Substring( j,1 ) );
i = j + 1;
}
SomeBytes = Encoding.UTF8.GetBytes( UrlEncoded.ToString() );
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write( SomeBytes, 0, SomeBytes.Length );
newStream.Close();
}
else
{
req.ContentLength = 0;
}
result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding( "utf-8" );
StreamReader sr = new StreamReader( ReceiveStream, encode );
Char[] read = new Char[256];
int count = sr.Read( read, 0, 256 );
while (count > 0)
{
String str = new String( read, 0, count );
count = sr.Read( read, 0, 256 );
strRetVal += str;
}
}
catch(Exception ex)
{
String s = ex.StackTrace;
}
finally
{
if ( result != null )
{
result.Close();
}
}
return strRetVal;
}

1
Tech Writer 2.2k 1.6m 22y i dont know if this is relavent, but I've been trying to do something similar. The datat that I sent never seemed to be reaching the location that i was sending it to. I tried sending the request to a local listener and dumped the results to screen, al the headers, the locations the final CRLF etc where all there, but for some reason the data was never sent with the message, i also found the same whemn using the webclient class, if i figure it out i'll let you know, but in the same vain can you let me know if you figure out what's not working, thanks