0

I have a webpage that need to pass data to web Service (SMS) and I tried to use WebRequest to do this work but when I use this class the following error appears:

Cannot close stream until all bytes are written 

in the line after this line :

stOut.WriteLine(strNewValue,number,text); 

What is the problem? I tried to use Flush() but didn't work either

public class SendSms { public SendSms(string number, string text) { string strNewValue; string strResponse; HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.buymessage.com/ostazSms/send.php"); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; strNewValue = "usr=****&pwd=*****&to={0}&msg={1}"; req.ContentLength = strNewValue.Length; using(StreamWriter stOut = new StreamWriter (req.GetRequestStream(), System.Text.Encoding.Unicode)) { stOut.WriteLine(strNewValue,number,text); } StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream()); strResponse = stIn.ReadToEnd(); stIn.Close(); } } 
3
  • is it a .net/WDSL webservice? It woudl probably be easier to create a Web reference or service reference and let it handle the http interaction. as for your approach, have you tried flushing the stream after the write? Commented Nov 2, 2012 at 23:32
  • @FrankThomas thanks for reply no it's not .net webservice Commented Nov 2, 2012 at 23:34
  • webservice site in php ? Commented Nov 2, 2012 at 23:35

1 Answer 1

1

Your issue is that you call WriteLine, which probably doesn't send all the data.

Here is a snippet that encodes the post data and sends it all:

string strNewValue = string.Format("usr=****&pwd=*****&to={0}&msg={1}", "A", "B"); byte[] byteArray = Encoding.UTF8.GetBytes (strNewValue); req.ContentLength = byteArray.Length; using (Stream dataStream = req.GetRequestStream ()) { dataStream.Write (byteArray, 0, byteArray.Length); } 
Sign up to request clarification or add additional context in comments.

8 Comments

but how i will add parameters into strNewValues ?
can i use string.format on strNewValues and add values then use the string in byteArray[]
Perform the string format first so you have your final string before you convert it into a byteArray.
thakyou so much no error is appear but the data is didnot pass to webservice .... can you help me in this :(
What information did you get back in the response? I would expect an HTTP response code that will help identify the issue.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.