如何编写 HTTP 请求 [英] How to write a HTTP Request

查看:19
本文介绍了如何编写 HTTP 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我尝试用 C# (Post) 编写 HTTP 请求,但我需要帮助解决错误

Hello I try to write a HTTP Request in C# (Post), but I need help with an error

Expl:我想将 DLC 文件的内容发送到服务器并接收解密的内容.

Expl: I want to send the Content of a DLC File to the Server and recive the decrypted content.

C# 代码

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

这里我收到了 html 请求

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>

错误信息:

{
    "form_errors": {
      "__all__": [
        "Sorry, an error occurred while processing the container."
       ]
    }
}

如果有人能帮我解决问题会很有帮助!

Would be very helpfull if someone could help me solving the problem!

推荐答案

您尚未设置内容长度,这可能会导致问题.您也可以尝试将字节直接写入流,而不是先将其转换为 ASCII ..(与您目前的做法相反),例如:

You 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:

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";

        byte[] _byteVersion = Encoding.ASCII.GetBytes(string.Concat("content=", dlc_content));

        request.ContentLength = _byteVersion.Length

        Stream stream = request.GetRequestStream();
        stream.Write(_byteVersion, 0, _byteVersion.Length);
        stream.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            Console.WriteLine(reader.ReadToEnd());
        }
    }

我个人认为过去这样的帖子有点烦躁".您也可以尝试在请求中设置 ProtocolVersion.

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.

这篇关于如何编写 HTTP 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆