当前位置: 首页 > 编程语言 > C#

如何编写HTTP请求分享

时间:2023-04-11 01:16:47 C#

C#学习教程:如何编写HTTP请求将DLC文件的内容发送到服务器并检索解密的内容。C#代码publicstaticvoiddecryptContainer(stringdlc_content){HttpWebRequestrequest=(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";使用(StreamWriterwriter=newStreamWriter(request.GetRequestStream(),Encoding.ASCII)){writer.Write("content="+dlc_content);}HttpWebResponse响应=(HttpWebResponse)request.GetResponse();使用(StreamReaderreader=newStreamReader(response.GetResponseStream())){Console.WriteLine(reader.ReadToEnd());}}这里我得到了html请求错误信息:{"form_errors":{"__all__":["Sorry,anerroroccurredwhileprocessingthecontainer."]}}如果有人可以帮助我解决我的问题,那将非常有帮助!您尚未设置内容长度,这可能会导致问题。您也可以尝试将字节直接写入流,而不先将其转换为ASCII。(这与您当前的工作方式相反),例如: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.LengthStreamstream=request.GetRequestStream();stream.Write(_byteVersion,0,_byteVersion.Length);stream.Close();HttpWebResponse响应=(HttpWebResponse)request.GetResponse();使用(StreamReaderreader=newStreamReader(response.GetResponseStream())){Console.WriteLine(reader.ReadToEnd());我个人觉得这样的帖子在过去有点“狡猾”。您也可以尝试根据请求设置ProtocolVersion。我会像这样简化你的代码:client.Headers[HttpRequestHeader.Accept]="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";字符串url="http://dcrypt.it/decrypt/paste";byte[]result=client.UploadValues(url,values);Console.WriteLine(Encoding.UTF8.GetString(结果));它还确保请求参数被正确编码。publicstringvoiddecryptContainer(stringdlc_content)//为什么不返回一个字符串,让调用者决定如何处理它。{HttpWebRequest请求=(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(StreamWriterwriter=newStreamWriter(request.GetRequestStream(),Encoding.ASCII)){writer.Write("content="+Uri.EscapeDataString(dlc_content));//转义dlc_content的值,这样发送的实体就是validapplication/x-www-form-urlencoded}using(HttpWebResponseresponse=(HttpWebResponse)request.GetResponse())//这应该在完成时处理。使用(StreamReaderreader=newStreamReader(response.GetResponseStream())){returnreader.ReadToEnd();}}实际发送的内容尽管存在问题我立即看到的一个问题是您需要对内容参数的值进行URL编码。使用HttpUtility.UrlEncode()。除此之外,可能还有其他问题,但不知道服务是做什么的就很难说。该错误过于笼统,可能意味着还应设置request.ContentLength。另外,你编码ASCII还是UTF8是有原因的吗?首先获取与响应关联的流并将其传递给Streamreader,如下所示:StreamreceiveStream=response.GetResponseStream();使用(StreamReaderreader=newStreamReader(receiveStream,Encoding.ASCII)){Console.WriteLine(reader.ReadToEnd());因为我无法评论JonHanna的解决方案。这解决了我:Uri.EscapeDataString以上就是C#学习教程:HTTP请求怎么写分享的全部内容。如果对大家有用,需要了解更多C#学习教程,希望大家多多关注——//thisiswhatwearesendingstringpost_data="content="+Uri.EscapeDataString(dlc_content);//这是我们发送它的地方stringuri="http://dcrypt.it/decrypt/paste";//创建请求HttpWebRequestrequest=(HttpWebRequest)WebRequest.Create(uri);请求.KeepAlive=false;request.ProtocolVersion=HttpVersion.Version10;request.Method="POST";//将我们的请求字符串转换为字节流byte[]postBytes=Encoding.ASCII.GetBytes(post_data);//这很重要-确保以这种方式指定类型request.ContentType="application/x-www-form-urlencoded";request.ContentLength=postBytes.Length;流requestStream=request.GetRequestStream();//现在发送它requestStream.Write(postBytes,0,postBytes.Length);requestStream.Close();//抓取响应并将其与状态代码一起打印到控制台HttpWebResponseresponse=(HttpWebResponse)request.GetResponse();Console.WriteLine(newStreamReader(response.GetResponseStream()).ReadToEnd());Console.WriteLine(response.StatusCode);本文收集自网络,不代表如有侵权等立场,请点击右边联系管理员删除。如需转载请注明出处: