/// <summary> /// 上传文件。formData参数附加到url /// </summary> /// <param name="formData"></param> /// <param name="filePath"></param> /// <returns></returns> public string Upload(FormData formData, string filePath) { var url = String.Format("{0}?{1}", Url, formData == null ? "" : formData.Format()); var data = new WebClient().UploadFile(url, "POST", filePath); return(Encoding.GetString(data)); }
/// <summary> /// 发送Get请求 /// </summary> /// <param name="formData"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public T Get <T>(FormData formData) { var request = WebRequest.Create(string.Format("{0}?{1}", Url, formData.Format())); request.Method = "GET"; T ret; using (var rep = request.GetResponse()) { ret = ReadFromResponse <T>(rep); } return(ret); }
/// <summary> /// 以post方式提交,将响应编码为字串。 /// </summary> /// <param name="body"></param> /// <param name="formData"></param> /// <returns></returns> public string PostString(string body, FormData formData = null) { var url = String.Format("{0}{1}", Url, formData == null ? "" : string.Format("?{0}", formData.Format())); return(PostString(url, body)); }
/// <summary> /// 以post方式提交,将响应编码为字串。 /// </summary> /// <param name="body"></param> /// <param name="formData"></param> /// <returns></returns> public string PostString(string body, FormData formData) { var request = CreateRequest(string.Format("{0}?{1}", Url, formData == null ? "" : formData.Format())); request.Method = "POST"; var sw = new StreamWriter(request.GetRequestStream());//注意,StreamWriter不能带编码的构造,否则会在前面写入编码标识(比utf8:ef bb bf) sw.Write(body); sw.Flush(); sw.Close();; using (var rep = request.GetResponse().GetResponseStream()) { var ret = ReadFromResponse(rep); return(ret); } }
public string GetString(FormData formData) { var url = string.Format("{0}?{1}", Url, formData.Format()); return(ReadFromResponse(GetStream(url))); }
public T Post <T>(string body, FormData formData = null) { var request = WebRequest.Create(string.Format("{0}?{1}", Url, formData == null ? "" : formData.Format())); request.Method = "POST"; var sw = new StreamWriter(request.GetRequestStream()); sw.WriteLine(body); sw.Flush(); sw.Close(); T ret; using (var rep = request.GetResponse()) { ret = ReadFromResponse <T>(rep); } return(ret); }