使用HttpWebRequest上传数据时出现问题

时间:2010-11-17 11:13:12

标签: c# .net asp.net

请你帮我解决我的问题。我是网络服务和HTTP的新手。

我写了以下代码来更新网站数据。代码运行;但如果上传,我无法看到我的数据。在这里,我们可以查看上传的数据,但我无法查看数据。

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

我在Code中提到的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

2 个答案:

答案 0 :(得分:0)

您可能会发现包含

有所帮助
requestStream.Flush();
.Close之前

Stream.Flush

答案 1 :(得分:0)

我没有看到实际获得响应的代码。缺少这种想法吗?

using (var r = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.UTF8))
    result = r.ReadToEnd();