2010-11-17 4 views
0

Просим вас помочь мне решить мою проблему. Я новичок в веб-сервисах и HTTP.проблема при отправке данных с помощью HttpWebRequest

Я написал следующий код для обновления данных на веб-сайте. Прогон кода; но я не могу видеть мои данные, если они загружены. Здесь у нас есть возможность увидеть, какие данные загружаются, но я не могу видеть свои данные.

 // Above URL is not real as I do not want to disclose real URL as of Now 
     Uri targetUrl = new Uri("http://www.x86map.com/post-embed/ewspost"); 

     HttpWebRequest request = null; 
     StringBuilder sb = new StringBuilder(); 
     Stream requestStream = null; 

     try 
     { 
       request = (HttpWebRequest)WebRequest.Create(targetUrl); 
       using (StreamReader inputReader = new StreamReader("C:\\SupportXml.xml")) 
       { 
         sb.Append(inputReader.ReadToEnd()); 
       } 

       String postData = sb.ToString(); 
       byte[] postDataBytes = Encoding.UTF8.GetBytes(postData); 

       request.Method = "POST"; 
       request.ContentType = "application/x-www-form-urlencoded"; 
       request.ContentLength = postDataBytes.Length; 
       request.KeepAlive = true; 
       request.Accept = "*/*"; 
       request.Headers.Add("Cache-Control", "no-cache"); 
       request.Headers.Add("Accept-Language", "en-us"); 
       request.Headers.Add("Accept-Encoding", "gzip,deflate"); 
       request.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8,q=0.66,*;q=0.66"); 

       requestStream = request.GetRequestStream(); 
       requestStream.Write(postDataBytes, 0, postDataBytes.Length); 
     } 

     catch (Exception ex) 
     { 
       Console.Write(ex.ToString()); 
     } 

     finally 
     { 
       if (null != requestStream) 
         requestStream.Close(); 
     }  

URL Я упоминал в Кодексе не реально. Пожалуйста, дайте мне знать, в чем проблема в моем коде. Ниже приведен код Java, который работает отлично. Я хочу преобразовать один и тот же код в C#.

 // Above URL is not real as I do not want to disclose real URL as of Now 
     String urlString = "http://www.x86map.com/post-embed/ewspost"; 
     StringBuffer s = new StringBuffer(); 

     try 
     { 
       String line = null; 
       BufferedReader input = new BufferedReader(new FileReader("C:\\SupportXml.xml")); 

       while ((line = input.readLine()) != null) 
       { 
         s.append(line); 
         s.append(System.getProperty("line.separator")); 
       } 

       String xmlDataString = s.toString(); 
       int length = xmlDataString.length(); 
       System.out.println("length " + length); 

       URL url = new URL(urlString); 
       System.out.println(url.toString()); 

       HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 

       connection.setRequestMethod("POST"); 
       connection.setDoOutput(true); 
       connection.setDoInput(true); 
       connection.setAllowUserInteraction(false); 
       connection.setUseCaches(false); 
       connection.setRequestProperty("Accept", "*/*"); 
       connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
       connection.setRequestProperty("Content-Length", (String.valueOf(length))); 
       connection.setRequestProperty("Cache-Control", "no-cache"); 
       connection.setRequestProperty("Accept-Language", "en-us"); 
       connection.setRequestProperty("Accept-Encoding", "gzip,deflate"); 
       connection.setRequestProperty("Connection", "Keep-Alive"); 
       connection.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8,q=0.66, *;q=0.66"); 

       BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream()); 
       BufferedReader reader = new BufferedReader(new StringReader(xmlDataString)); 


       System.out.println("Proxy Used :" + connection.usingProxy()); 

       int dataRead; 
       bos.write("XML_string=".getBytes()); 
       while ((dataRead = reader.read()) != -1) 
       { 
         bos.write(dataRead); 
       } 
       bos.flush(); 
       bos.close(); 

       BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); 

       String res = null; 
       while ((res = br.readLine()) != null) 
       { 

       } 
       br.close(); 
     } 

     catch (IOException e) 
     { 
       e.printStackTrace(); 
     } 

Пожалуйста, помогите мне решить эту проблему.

Спасибо и наилучшими пожеланиями, map125

ответ

0

Вы можете найти это помогает включить

requestStream.Flush(); 

перед тем .Close ИНГ его.

Stream.Flush

0

Я не вижу код, который на самом деле получает ответ. Это желание отсутствует?

using (var r = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.UTF8)) 
    result = r.ReadToEnd(); 
Смежные вопросы