-1

I need to implement an application to post string like HttpRequest to specify IP address and then receive answer in httpRespons form. Here is code for sending

WebRequest request = WebRequest.Create("http://192.168.1.10");
request.Method = "POST";
string postData = "string I want to post";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

I'm not able to test this code. Is this code OK?

Here is code for receive answer:

WebResponse response = request.GetResponse();
richTextBox1.AppendText(((HttpWebResponse)response).StatusDescription);
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
richTextBox1.AppendText(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();

Is this code OK?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
MarekD
  • 1

1 Answers1

0

The code is correct,but i think StreamReader reader = new StreamReader(dataStream); should append Encoding :StreamReader reader = new StreamReader(dataStream,encoding);

John Paul
  • 12,196
  • 6
  • 55
  • 75