httpwebrequest将参数传递给Web服务



我有一个需要将数据传递给Web服务(SMS)的网页,并且我尝试使用WebRequest进行此工作,但是当我使用此类别时,出现以下错误:

Cannot close stream until all bytes are written

在此行之后的行中:

stOut.WriteLine(strNewValue,number,text);

有什么问题?我尝试使用Flush(),但也没有工作

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

您的问题是您致电WriteLine,可能不会发送所有数据。

这是一个编码帖子数据并将其发送的片段:

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

相关内容

  • 没有找到相关文章

最新更新