开发者

problem in uploading data with HttpWebRequest

Request you to please help me to solve my problem. I am new to web-services and HTTP.

I wrote following code to update data to website. Code run; but I am not able to see my data if uploaded. Here we have facility to see what data is getting uploaded but I am not able to see my data.

      // 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 I mentioned in Code is not real. Please let me know what is the problem in my code. Following is the Java code working perfect. I want to convert same code in 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);
               con开发者_如何学编程nection.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();
      }

Please help me to resolve this issue.

Thanks and Regards, map125


You may find it helps to include

requestStream.Flush();

before .Closeing it.

Stream.Flush


I do not see the code that actually gets the response. Is this want is missing?

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜