2011-10-26 15 views
13

हैलो मैं सी # (पोस्ट) में एक HTTP अनुरोध लिखने की कोशिश, लेकिन मैं एक त्रुटिकैसे एक HTTP अनुरोध लिखने के लिए

Expl साथ मदद की जरूरत: मैं सर्वर से एक डीएलसी फ़ाइल की सामग्री भेजना चाहते हैं और डिक्रिप्टेड सामग्री को दोबारा दोहराएं।

सी # कोड

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

और यहाँ मैं एचटीएमएल अनुरोध

मिला
<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()); 
     } 
    } 

मैं व्यक्तिगत रूप से है अतीत में इस तरह की पोस्टिंग को थोड़ा "विचित्रता" पाया गया। आप अनुरोध पर प्रोटोकॉल वर्जन सेट करने का भी प्रयास कर सकते हैं।

0

प्रतिक्रिया के साथ जुड़े धारा पहले तो पारित हो जाओ कि StreamReader में नीचे के रूप में:

Stream receiveStream = response.GetResponseStream();  

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

एक मुद्दा मैं अभी देखते हैं कि आप content पैरामीटर का एनकोड मूल्य URL की आवश्यकता है। इसके लिए 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

आपने किस वेब क्लाइंट का उपयोग किया था? –

1

अनुरोध। सामग्री लम्बाई भी सेट की जानी चाहिए।

इसके अलावा, क्या आप एक कारण है कि आप ASCII एन्कोडिंग बनाम यूटीएफ 8 हैं?

+0

HttpWebRequest डिफ़ॉल्ट रूप से ContentLength सेट करता है। एप्लिकेशन/एक्स-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

जैसा कि मैं जॉन हन्ना के समाधान पर टिप्पणी नहीं कर सकता। यह मेरे लिए इसे हल: 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);