当前位置: 首页 > 工具软件 > HTTPCWS > 使用案例 >

ASP.NET访问httpcws的分词系统,取得返回的结果。

梁研
2023-12-01
#region 获取关键字
    [AjaxPro.AjaxMethod]
    public string GetWords(string str)
    {
        string _value = string.Empty;
        byte[] gbkBuf = Encoding.GetEncoding("GBK").GetBytes(str);
        _value = Encoding.GetEncoding("GBK").GetString(gbkBuf);

        string uri = "http://fenci.haedu.cn:1985/?w="+ HttpUtility.UrlEncode(str,Encoding.GetEncoding("GBK"));//这一定要用GBK编码,因为httpcws用的是GBK的编码,郁闷了好几天终于搞定了。另外这里也要用HttpUtility.UrlEncode而不能用Server.UrlEncode要不然也是乱码。因为Server.UrlEncode是4字节编码(UTF8),HttpUtility.UrlEncode用的系统默认编码,2字节。
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
        req.Method = "GET";
        req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; TencentTraveler 4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)";
        HttpWebResponse res = req.GetResponse() as HttpWebResponse;

        Stream ReceiveStream = res.GetResponseStream();
        StreamReader sr = new StreamReader(ReceiveStream, Encoding.Default);
        _value = HttpUtility.UrlDecode(sr.ReadToEnd());

        sr.Close(); 
        ReceiveStream.Close();
        res.Close();        
        return _value;
    }
#endregion

 

 类似资料: