Reputation: 363
I am able to send the first request working fine, however I can't get my head round why it stalls on getting the Stream os = smsRequest.GetRequestStream()
the second time.
I am aware that you can't write to a Request more than once that is why a new instance is created each time.
public void SendSMS(Dictionary<double, IList<string>> texts)
{
if (CreateWebRequest())
{
foreach (double mpn in texts.Keys)
{
foreach (string sms in texts[mpn])
{
string formParams = string.Format("sendTo=0{0}&selectText=Please+Select...&textMessage={1}&x=28&y=10", mpn, sms);
byte[] encodedParams = Encoding.UTF8.GetBytes(formParams);
HttpWebRequest smsRequest = CreateSMSRequest(encodedParams);
using (Stream os = smsRequest.GetRequestStream())
{
os.Write(encodedParams, 0, encodedParams.Length);
os.Close();
}
}
}
}
}
private HttpWebRequest CreateSMSRequest(byte[] encodedParams)
{
HttpWebRequest smsRequest = (HttpWebRequest)WebRequest.Create(PostUrl);
smsRequest.Method = WebRequestMethods.Http.Post;
smsRequest.ContentType = "application/x-www-form-urlencoded";
smsRequest.ContentLength = encodedParams.Length;
smsRequest.AllowAutoRedirect = false;
smsRequest.Credentials = CredentialCache.DefaultNetworkCredentials;
smsRequest.Headers.Add(HttpRequestHeader.Cookie, _cookieData);
return smsRequest;
}
Upvotes: 0
Views: 2191
Reputation: 548
I think your answer is the same as this one: HttpWebRequest getRequestStream hangs on multiple runs
After your using statement put:
var response = smsRequest.GetResponse() as HttpWebResponse;
Upvotes: 1