2010-11-17 63 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();