.NET Framework Bookmark and Share   
 index > Network Class Library (System.Net) > How to set encoding of the send data of an HttpWebRequest
 

How to set encoding of the send data of an HttpWebRequest

result = file_get_contents(
 mosms_url
 + "?username=" + mosms_username
 + "&password=" + mosms_password
 + "&type=" + "text"
 + "&nr=" + m_View.PhoneNr
 + "&data=" + mosms_data
);
private String file_get_contents(String url)
{
 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url) ;
 WebResponse response = request.GetResponse();
 Stream stream = response.GetResponseStream();
 StreamReader streamReader = new StreamReader(stream);
 string responseText = streamReader.ReadToEnd();
 streamReader.Close();
 stream.Close();
 response.Close();
 return responseText;
}
//Tried with these lines without success.
request.SendChunked = true;
request.TransferEncoding = "ISO-8859-1";


One part of my application connects to an sms/text-service.
You provide a few variables, send the data over a HttpWebRequest and get a responseif it was a successful sending.
The messages are sent with encoding ISO 8859-1 and it is what I have a problem with since I dont exactly know how to provide this encoding to them.
I have tried to set the encoding as the last two lines in the code block, but that failed.
When calling response.GetResponseStream();
I got an exception that complained about SendChunked has to be true although I had set that property.
I feel that I am missing something essential, any help is highly appreciated.sandsater

Mattias Sandsäter
I laborated with that without any success.
However, I realized that the only thing I had to do to get it work was

Encoding enc = Encoding.GetEncoding("ISO-8859-1");
String encData = HttpUtility.UrlEncode(sms_data, enc);

So now it works with all kinds of characters in the message.


sandsater
Mattias Sandsäter
From looking at your code, it seems that you are posting the data as Query parameters of the URL. In that case, TransferEncoding & SendChunked are not needed.

These are only needed if you are sending data in the entity body, and need to specify a transfer encoding.

If you had to send data in entity body, you can encode it with ISO-8859-1 as follows:

Encoding enc = Encoding.GetEncoding("ISO-8859-1");
int index = url.IndexOf("?");
byte [] body = null;
if (index != -1)
{
    String query = url.Substring(index);
    body = enc.GetBytes(query);
}

Stream requestStream = request.GetRequestStream();
requestStream.Write(body, 0, body.Length);
requestStream.Close();

HttpWebResponse response = ...


feroze
--
My blog
Feroze Daud
I laborated with that without any success.
However, I realized that the only thing I had to do to get it work was

Encoding enc = Encoding.GetEncoding("ISO-8859-1");
String encData = HttpUtility.UrlEncode(sms_data, enc);

So now it works with all kinds of characters in the message.


sandsater
Mattias Sandsäter

You can use google to search for other answers

Custom Search

More Threads

• Post Data to different domain - ASP.Net/C#
• Welcome to the forum!
• Ping in Thread (C#)
• Checking if remote ip address is connected to the server
• UDP Multicast Dgram size question
• OleDbCommand.ExecuteReader() Error
• Solve RRAS Hangup Problem in .Net 1.1
• SSL with Socket connection.
• HttpWebRequest Basic Authentication fails in C# windows program.
• How to access mailbox inbox with use of CDO?