string url = "http://http.sms0551.com/tx/?uid=82612&pwd=123456&mobile=" + phone + "&content=" + body;//body中包含中文
string result = ObtainHtml2(url);
private static string ObtainHtml2(string url)
{
string strHtml;
try
{
WebClient wc = new WebClient();
Byte[] bHtml = wc.DownloadData(url);
strHtml = System.Text.Encoding.UTF8.GetString(bHtml);
}
catch
{
strHtml = "";
}
return strHtml;
}
这是一个http请求,她能向手机发送一个短信,因为短信中的内容包含有中文,导致手机看到的是乱码,现在服务器端该做何处理。其中我发现HttpUtility.UrlEncode,HttpUtility.UrlDecode,Server.UrlEncode,Server.UrlDecode在控制台应用程序都不能用。问题是能用该怎么用,不能用该采取什么办法
string result = ObtainHtml2(url);
private static string ObtainHtml2(string url)
{
string strHtml;
try
{
WebClient wc = new WebClient();
Byte[] bHtml = wc.DownloadData(url);
strHtml = System.Text.Encoding.UTF8.GetString(bHtml);
}
catch
{
strHtml = "";
}
return strHtml;
}
这是一个http请求,她能向手机发送一个短信,因为短信中的内容包含有中文,导致手机看到的是乱码,现在服务器端该做何处理。其中我发现HttpUtility.UrlEncode,HttpUtility.UrlDecode,Server.UrlEncode,Server.UrlDecode在控制台应用程序都不能用。问题是能用该怎么用,不能用该采取什么办法
本内容使用CSDN小秘书回复
每天回帖即可得10分可用分!
*****************************************[/align]
//发送的时候
var body = "我是中文的";
var base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(body));
string url = "http://http.sms0551.com/tx/?uid=82612&pwd=123456&mobile=" + phone + "&content=" + base64 ;//body中包含中文
string result = ObtainHtml2(url);
//接收的时候private static string ObtainHtml2(string url)
{
string strHtml;
try
{
WebClient wc = new WebClient();
Byte[] bHtml = wc.DownloadData(url); strHtml = System.Text.Encoding.UTF8.GetString(bHtml);
var body2 = Encoding.UTF8.GetString(Convert.FromBase64String(strHtml));
} catch
{
strHtml = "";
}
return strHtml;
}
回复二楼:我把strHtml = System.Text.Encoding.UTF8.GetString(bHtml)改成gb2312的编码也不行
回复三楼:没成功,倒是把中文转成ASCII码了
控制台程序需要添加 System.Web.dll
//那个网站的开发文档
static void Main(string[] args)
{
string strContent = "中国短信网测试c#";
//GET 方式
String getReturn = doGetRequest("http://http.c123.com/tx/?uid=9999&pwd=fa246d0262c3925617b0c72bb20eeb1d&mobile=13585519197,13900008888&content=" + strContent);
Console.WriteLine("Get response is: " + getReturn);
StringBuilder sbTemp = new StringBuilder();
//POST
sbTemp.Append("uid=9999&pwd=fa246d0262c3925617b0c72bb20eeb1d&mobile=13585519197,13900008888&content=" + strContent);
byte[] bTemp = System.Text.Encoding.GetEncoding("GBK").GetBytes(sbTemp.ToString());
String postReturn = doPostRequest("http://http.c123.com/tx/", bTemp);
Console.WriteLine("Post response is: " + postReturn); } //POST方式发送得结果
private static String doPostRequest(string url, byte[] bData)
{
System.Net.HttpWebRequest hwRequest;
System.Net.HttpWebResponse hwResponse; string strResult = string.Empty;
try
{
hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
hwRequest.Timeout = 5000;
hwRequest.Method = "POST";
hwRequest.ContentType = "application/x-www-form-urlencoded";
hwRequest.ContentLength = bData.Length; System.IO.Stream smWrite = hwRequest.GetRequestStream();
smWrite.Write(bData, 0, bData.Length);
smWrite.Close();
}
catch (System.Exception err)
{
WriteErrLog(err.ToString());
return strResult;
} //get response
try
{
hwResponse = (HttpWebResponse)hwRequest.GetResponse();
StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.ASCII);
strResult = srReader.ReadToEnd();
srReader.Close();
hwResponse.Close();
}
catch (System.Exception err)
{
WriteErrLog(err.ToString());
} return strResult;
}
//GET方式发送得结果
private static String doGetRequest(string url)
{
HttpWebRequest hwRequest;
HttpWebResponse hwResponse; string strResult = string.Empty;
try
{
hwRequest = (System.Net.HttpWebRequest)WebRequest.Create(url);
hwRequest.Timeout = 5000;
hwRequest.Method = "GET";
hwRequest.ContentType = "application/x-www-form-urlencoded";
}
catch (System.Exception err)
{
WriteErrLog(err.ToString());
return strResult;
} //get response
try
{
hwResponse = (HttpWebResponse)hwRequest.GetResponse();
StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.ASCII);
strResult = srReader.ReadToEnd();
srReader.Close();
hwResponse.Close();
}
catch (System.Exception err)
{
WriteErrLog(err.ToString());
} return strResult;
} private static void WriteErrLog(string strErr)
{
Console.WriteLine(strErr);
System.Diagnostics.Trace.WriteLine(strErr);
}