How to write a HTTP Request

2020-02-04 15:33发布

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!

7条回答
贼婆χ
2楼-- · 2020-02-04 16:12
public string void decryptContainer(string dlc_content) //why not return a string, let caller decide what to do with it.
{
    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";//sure this is needed? Maybe just match the one content-type you expect.
    using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
    {
        writer.Write("content=" + Uri.EscapeDataString(dlc_content));//escape the value of dlc_content so that the entity sent is valid application/x-www-form-urlencoded
    }
    using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())//this should be disposed when finished with.
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        return reader.ReadToEnd();
    }
}

There could still be a problem with what is actually being sent.

查看更多
登录 后发表回答