Hello I try to write a HTTP Request in C# (Post), but I need help with an error
Expl: I want to send the Content of a DLC File to the Server and recive the decrypted content.
C# Code
public static void decryptContainer(string dlc_content)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://dcrypt.it/decrypt/paste");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
{
writer.Write("content=" + dlc_content);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
Console.WriteLine(reader.ReadToEnd());
}
}
and here I got the html request
<form action="/decrypt/paste" method="post">
<fieldset>
<p class="formrow">
<label for="content">DLC content</label>
<input id="content" name="content" type="text" value="" />
</p>
<p class="buttonrow"><button type="submit">Submit »</button></p>
</fieldset>
</form>
Error Message:
{
"form_errors": {
"__all__": [
"Sorry, an error occurred while processing the container."
]
}
}
Would be very helpfull if someone could help me solving the problem!
I would simplify your code, like this:
It also ensures that the request parameters are properly encoded.
One issue I see right away is that you need to URL encode value of the
content
parameter. UseHttpUtility.UrlEncode()
for that. Other than that there might be other issues there, but it is hard to say not knowing what service does. The error is too generic and could mean anythingYou haven't set a content-length, which might cause issues. You could also try writing bytes directly to the stream instead of converting it to ASCII first.. (do it the opposite way to how you're doing it at the moment), eg:
I've personally found posting like this to be a bit "fidgity" in the past. You could also try setting the ProtocolVersion on the request.
As I can't comment on the solution of Jon Hanna. This solved it for me: Uri.EscapeDataString
request.ContentLength should be set as well.
Also, is there a reason you are ASCII encoding vs UTF8?
Get the stream associated with the response first then pass that into the Streamreader as below: