如何编写 HTTP 请求
您好,我尝试用 C#(Post)编写 HTTP 请求,但我需要错误
Expl 的帮助:我想将 DLC 文件的内容发送到服务器并接收解密的内容。
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 请求
<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."
]
}
}
如果有人可以帮助我解决问题,将会非常有帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您尚未设置内容长度,这可能会导致问题。您也可以尝试直接将字节写入流,而不是先将其转换为 ASCII。(与您目前的做法相反),例如:
我个人发现像这样的发帖是过去有点“烦躁”。您还可以尝试在请求上设置 ProtocolVersion。
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:
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.
我会简化您的代码,如下所示:
它还确保请求参数被正确编码。
I would simplify your code, like this:
It also ensures that the request parameters are properly encoded.
实际发送的内容可能仍然存在问题。
There could still be a problem with what is actually being sent.
我立即发现的一个问题是您需要对
content
参数的值进行 URL 编码。为此,请使用 HttpUtility.UrlEncode() 。除此之外可能还有其他问题,但很难说不知道服务的作用。该错误太笼统,可能意味着任何事情
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 anything
request.ContentLength 也应该设置。
另外,你选择 ASCII 编码还是 UTF8 编码有什么原因吗?
request.ContentLength should be set as well.
Also, is there a reason you are ASCII encoding vs UTF8?
首先获取与响应关联的流,然后将其传递到 Streamreader 中,如下所示:
Get the stream associated with the response first then pass that into the Streamreader as below:
因为我无法评论乔恩·汉纳的解决方案。这为我解决了这个问题:
Uri.EscapeDataString
As I can't comment on the solution of Jon Hanna. This solved it for me:
Uri.EscapeDataString