简单的方案,创建一个WebClient对象,封装了很多。
复杂点的,使用HttpRequest来访问(WebClient就是一个封装)。
Encoding code = Encoding.GetEncoding("utf-8"); //待请求参数数组字符串 string strRequestData = ""; //把数组转换成流中所需字节数组类型 byte[] bytesRequestData = code.GetBytes(strRequestData); //构造请求地址 string strUrl = "http://www.xxx.com/api/xxx.do"; //请求远程HTTP string strResult = ""; try { //设置HttpWebRequest基本信息 HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl); myReq.Method = "post"; myReq.ContentType = "application/x-www-form-urlencoded"; //填充POST数据 myReq.ContentLength = bytesRequestData.Length; Stream requestStream = myReq.GetRequestStream(); requestStream.Write(bytesRequestData, 0, bytesRequestData.Length); requestStream.Close(); //发送POST数据请求服务器 HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse(); Stream myStream = HttpWResp.GetResponseStream(); //获取服务器返回信息 StreamReader reader = new StreamReader(myStream, code); StringBuilder responseData = new StringBuilder(); String line; while ((line = reader.ReadLine()) != null) { responseData.Append(line); } //释放 myStream.Close(); strResult = responseData.ToString(); } catch (Exception exp) { strResult = "报错:" + exp.Message; }
jsonp
webservice
webservice
使用HttpWebRequest或WebClient,将Method设置成POST,要提交的参数拼接成字符串用文件流写进去就是了。Java端可以获取到的。记得设置表单属性。
看,各种虾,各种鸟都出来了。方法够用了吧。