2011-10-26 74 views
13

你好,我试着写在C#(POST)一个HTTP请求,但我需要一个错误如何写一个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." 
     ] 
    } 
} 

将是非常有益的,如果有人可以帮助我解决这个问题!

+0

http://codesamplez.com/programming/http-request-c-sharp – happy

回答

14

您尚未设置可能导致问题的内容长度。您也可以尝试直接写入字节流的转换,而不是它第一次为ASCII。(这样做是为了你如何在此刻做相反的方式),例如:

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

我个人发现这样的发布在过去有点“fidgity”。您也可以尝试在请求上设置ProtocolVersion。

0

获取与响应相关的物流第一,然后传递到StreamReader的,如下:

Stream receiveStream = response.GetResponseStream();  

    using (StreamReader reader = new StreamReader(receiveStream, Encoding.ASCII)) 
    { 
     Console.WriteLine(reader.ReadToEnd()); 
    } 
1

一个问题我马上看到的是,你需要的URL参数content的编码值。为此,请使用HttpUtility.UrlEncode()。 除此之外,可能还有其他问题,但很难说不知道服务是什么。该错误是过于笼统,可能意味着什么

6

我将简化你的代码,就像这样:

public static void decryptContainer(string dlc_content) 
{ 
    using (var client = new WebClient()) 
    { 
     var values = new NameValueCollection 
     { 
      { "content", dlc_content } 
     }; 
     client.Headers[HttpRequestHeader.Accept] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; 
     string url = "http://dcrypt.it/decrypt/paste"; 
     byte[] result = client.UploadValues(url, values); 
     Console.WriteLine(Encoding.UTF8.GetString(result)); 
    } 
} 

这也确保了请求参数编码正确。

+0

它还确保参数进行编码或多或少正常。请参阅http://stackoverflow.com/a/10836145/4136325 –

+0

非常轻量级的示例。谢谢。 +1 –

+0

你使用了哪个web客户端? –

1

request.ContentLength也应该设置。

此外,有没有理由你是ASCII编码与UTF8?

+0

默认情况下,HttpWebRequest设置ContentLength本身。 application/x-www-form-urlencoded将始终在ASCII范围内,所以在两种编码之间都是相同的。 –

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

实际发送的内容仍然存在问题。

0

由于我不能评论Jon Hanna的解决方案。这解决了这个问题对我来说: Uri.EscapeDataString

 // this is what we are sending 
     string post_data = "content=" + Uri.EscapeDataString(dlc_content); 

     // this is where we will send it 
     string uri = "http://dcrypt.it/decrypt/paste"; 

     // create a request 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 
     request.KeepAlive = false; 
     request.ProtocolVersion = HttpVersion.Version10; 
     request.Method = "POST"; 

     // turn our request string into a byte stream 
     byte[] postBytes = Encoding.ASCII.GetBytes(post_data); 

     // this is important - make sure you specify type this way 
     request.ContentType = "application/x-www-form-urlencoded"; 
     request.ContentLength = postBytes.Length; 

     Stream requestStream = request.GetRequestStream(); 

     // now send it 
     requestStream.Write(postBytes, 0, postBytes.Length); 
     requestStream.Close(); 

     // grab te response and print it out to the console along with the status code 
     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
     Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd()); 
     Console.WriteLine(response.StatusCode);