关于javascript调用webservices的中文参数乱码的问题

我写了个webservices的方法~通过城市名获取天气
我如果直解在服务器端调用不会出现乱码问题~(我javascirpt是下了微软的webservices.htc)
可我在javascript调用webservices中文参数变成????,我也试过很多办法多没用包括在web,config定义utf-8什么的也没用~我现在把代码发出来,希望大家帮忙
[WebMethod]
    public string GetWeather(string city)
    {
        string weacherhtml = String.Empty;
        //转换输入参数的编码类型

        string mycity = System.Web.HttpUtility.UrlEncode(city, System.Text.UnicodeEncoding.GetEncoding("GB2312"));
        //初始化新的 WebRequest
        HttpWebRequest webrt = (HttpWebRequest)WebRequest.Create("http://php.weather.sina.com.cn/search.php?city=" + mycity);
        //返回对 Internet 请求的响应。
        HttpWebResponse webrs = (HttpWebResponse)webrt.GetResponse();

        //从 Internet 资源返回数据流。
        Stream stream = webrs.GetResponseStream();

        //读取数据流
        StreamReader srm = new StreamReader(stream, System.Text.Encoding.Default);
        //从头读到尾,把数据读到weacherhtml中
        weacherhtml = srm.ReadToEnd();
        //关闭打开的资源
        srm.Close();
        stream.Close();
        webrs.Close();
        //针对不同的网站,以下开始部分和结束部分不同。
        //可通过查看网站的源文件解决。
        int start = weacherhtml.IndexOf("<!-- 天气状况 begin -->");
        int end = weacherhtml.IndexOf("<!-- 天气状况 end -->");
        //返回一个HTML的Table,预报城市天气
        return weacherhtml.Substring(start, end - start);
    }

javascript
   <script language="javascript" type="text/javascript">
    var iCallID=0;
    function init()
    {
        service.useService("http://localhost:4897/WebServices/luca.asmx?WSDL","MYPath");
        var drp=document.getElementById("<%=DropDownList1.ClientID %>").value;
       
        iCallID=service.MYPath.callService(onResult,"GetWeather",drp);
       
    }
    function onResult(result)
    {
        if(!result.error)
        {
           
           divResult.innerText=result.value;
        }
    }

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="service" style="BEHAVIOR:url(webservice.htc)">
            &nbsp;<div style="text-align: left">
                <table>
                    <tr>
                        <td style="width: 100px">
            <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem Value="北京">北京</asp:ListItem>
                <asp:ListItem Value="上海">上海</asp:ListItem>
            </asp:DropDownList></td>
                        <td style="width: 100px">
                            <input id="Button1" type="button" value="查询" onclick="init()" /></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                        <div id="divResult" style="width:200px; height:300px; overflow:auto">
                       
                        </div>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
    </form>
</body>
</html>

你可能感兴趣的:(WebServices)