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