/// <summary> /// 请求访问类 /// </summary> public class ServletMgr { /// <summary> /// 发送请求到servlet并得到返回结果 /// </summary> /// <param name="strRomoteUrl">请求的Servlet</param> /// <param name="strErrorMsg">返回的错误描述,如果无错误,则返回空字符串</param> /// <returns>返回servlet处理后的结果</returns> public static CookieContainer severCookie = null; public static String getServletResult(String strRomoteUrl, String pincode, String mobile, String signcode, String ip, String ipdesc, Int32 fid, Int32 bid) { String strErrorMsg = ""; String result = ""; //创建请求对象 //WebRequest request = WebRequest.Create(_url); //request.Credentials = new NetworkCredential("invaliduser", "invalidpassword"); //创建响应对象 //WebResponse response =request.GetResponse(); try { //创建请求对象 WebRequest request = WebRequest.Create(strRomoteUrl); //request.Credentials = new NetworkCredential("invaliduser", "invalidpassword"); if (severCookie == null) { severCookie = new CookieContainer(); ((HttpWebRequest)request).CookieContainer = severCookie; } else { ((HttpWebRequest)request).CookieContainer = severCookie; } //创建响应对象 WebResponse response = request.GetResponse(); //成功解析 Uri //从服务器获取可读的流 Stream ReceiveStream = response.GetResponseStream(); Encoding encode = System.Text.Encoding.Default; StreamReader sr = new StreamReader(ReceiveStream, encode); Char[] read = new Char[256]; Int32 count = sr.Read(read, 0, 256); while (count > 0) { String str = new String(read, 0, count); result = result + str; count = sr.Read(read, 0, 256); } } catch (WebException WebExcp) { // 获取 WebException 状态码 if (WebExcp.Status == WebExceptionStatus.ProtocolError) // 7 指示协议错误,因此 WebResonse 对象应该存在 { // 写出 WebResponse 协议状态 strErrorMsg = "检索 URI 时出错!详细错误:" + WebExcp.Message.ToString(); return ""; } else { strErrorMsg = WebExcp.Message.ToString(); return ""; } } catch (UriFormatException UriExcp) { strErrorMsg = "已捕获 URIFormatException!详细错误:" + UriExcp.Message.ToString(); return ""; } catch (Exception exc) { strErrorMsg = exc.Message.ToString(); return ""; } return result; } public static String getServletResult(String strRomoteUrl) { String strErrorMsg = ""; String result = ""; //创建请求对象 //WebRequest request = WebRequest.Create(_url); //request.Credentials = new NetworkCredential("invaliduser", "invalidpassword"); //创建响应对象 //WebResponse response =request.GetResponse(); try { //创建请求对象 WebRequest request = WebRequest.Create(strRomoteUrl); //request.Credentials = new NetworkCredential("invaliduser", "invalidpassword"); if (severCookie == null) { severCookie = new CookieContainer(); ((HttpWebRequest)request).CookieContainer = severCookie; } else { ((HttpWebRequest)request).CookieContainer = severCookie; } //创建响应对象 WebResponse response = request.GetResponse(); //成功解析 Uri //从服务器获取可读的流 Stream ReceiveStream = response.GetResponseStream(); Encoding encode = System.Text.Encoding.Default; StreamReader sr = new StreamReader(ReceiveStream, encode); Char[] read = new Char[256]; Int32 count = sr.Read(read, 0, 256); while (count > 0) { String str = new String(read, 0, count); result = result + str; count = sr.Read(read, 0, 256); } } catch (WebException WebExcp) { // 获取 WebException 状态码 if (WebExcp.Status == WebExceptionStatus.ProtocolError) // 7 指示协议错误,因此 WebResonse 对象应该存在 { // 写出 WebResponse 协议状态 strErrorMsg = "检索 URI 时出错!详细错误:" + WebExcp.Message.ToString(); return ""; } else { strErrorMsg = WebExcp.Message.ToString(); return ""; } } catch (UriFormatException UriExcp) { strErrorMsg = "已捕获 URIFormatException!详细错误:" + UriExcp.Message.ToString(); return ""; } catch (Exception exc) { strErrorMsg = exc.Message.ToString(); return ""; } return result; } public static String UTF8ToGB(String utfinfo) { String gb2312info = String.Empty; Encoding utf8 = Encoding.UTF8; ; Encoding gb2312 = Encoding.GetEncoding("gb2312"); Byte[] unicodebytes = utf8.GetBytes(utfinfo); byte[] asciibytes = Encoding.Convert(utf8, gb2312, unicodebytes); for (Int32 i = 0; i < asciibytes.Length; i++) { gb2312info += "%" + Convert.ToString(asciibytes[i], 16); } return gb2312info; } }