2011-10-26 61 views
13

Xin chào Tôi cố gắng viết Yêu cầu HTTP trong C# (Bài đăng), nhưng tôi cần trợ giúp với lỗiCách viết Yêu cầu HTTP

Giải thích: Tôi muốn gửi Nội dung của Tệp DLC đến Máy chủ và recive nội dung giải mã.

C# Mã

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

và ở đây tôi đã yêu cầu 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> 

Thông báo Lỗi:

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

Sẽ rất hữu ích nếu ai đó có thể giúp tôi giải quyết vấn đề!

+0

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

Trả lời

14

Bạn chưa đặt độ dài nội dung, điều này có thể gây ra sự cố. Bạn cũng có thể thử viết byte trực tiếp vào dòng thay vì chuyển đổi nó để ASCII đầu tiên .. (làm theo cách ngược lại với cách bạn đang làm việc đó vào lúc này), ví dụ:

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

Tôi đã đích thân tìm thấy bài đăng như thế này là một chút "fidgity" trong quá khứ. Bạn cũng có thể thử đặt ProtocolVersion theo yêu cầu.

0

Get dòng liên quan đến phản ứng đầu tiên sau đó vượt qua đó vào StreamReader như sau:

Stream receiveStream = response.GetResponseStream();  

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

Một vấn đề tôi thấy ngay lập tức là bạn cần phải URL giá trị mã hóa của tham số content. Sử dụng HttpUtility.UrlEncode() cho điều đó. Khác với điều đó có thể có các vấn đề khác ở đó, nhưng rất khó để nói không biết những gì dịch vụ nào. Lỗi này là quá chung chung và có thể có nghĩa là bất cứ điều gì

6

tôi sẽ đơn giản hóa mã của bạn, như thế này:

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

Nó cũng đảm bảo rằng các thông số yêu cầu được mã hóa đúng cách.

+0

Nó cũng đảm bảo rằng các thông số được mã hóa nhiều hay ít đúng cách. Xem http://stackoverflow.com/a/10836145/4136325 –

+0

Ví dụ rất nhẹ. Cảm ơn bạn. +1 –

+0

Bạn đã sử dụng webClient nào? –

1

request.ContentLength cũng nên được đặt.

Ngoài ra, có lý do nào bạn mã hóa ASCII so với UTF8 không?

+0

HttpWebRequest đặt ContentLength theo mặc định. ứng dụng/x-www-form-urlencoded sẽ luôn nằm trong phạm vi ASCII, vì vậy nó giống hệt nhau giữa cả hai mã hóa. –

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

Vẫn có thể có vấn đề với những gì thực sự được gửi.

0

Vì tôi không thể nhận xét về giải pháp của Jon Hanna. Đây giải quyết nó cho tôi: 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);