|
|
导航: |
论坛 -> Web应用开发
斑竹:bodies |
|
作者: |
ozhy1 (ozhy1) |
★☆☆☆☆ |
-
|
普通会员 |
|
2017/8/13 20:31:51 |
标题: |
哪位大侠能把这段java代码翻译成delphi的呢? |
浏览:1891 |
|
加入我的收藏 |
楼主: |
const String host = "https://ali-weather.showapi.com"; private const String path = "/phone-post-code-weeather"; private const String method = "GET"; private const String appcode = "你自己的AppCode";
static void Main(string[] args) { String querys = "need3HourForcast=0&needAlarm=0&needHourData=0&needIndex=0&needMoreDay=0&phon_e_code=021&post_code=200000"; String bodys = ""; String url = host + path; HttpWebRequest httpRequest = null; HttpWebResponse httpResponse = null;
if (0 < querys.Length) { url = url + "?" + querys; }
if (host.Contains("https://")) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url)); } else { httpRequest = (HttpWebRequest)WebRequest.Create(url); } httpRequest.Method = method; httpRequest.Headers.Add("Authorization", "APPCODE " + appcode); if (0 < bodys.Length) { byte[] data = Encoding.UTF8.GetBytes(bodys); using (Stream stream = httpRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } } try { httpResponse = (HttpWebResponse)httpRequest.GetResponse(); } catch (WebException ex) { httpResponse = (HttpWebResponse)ex.Response; }
Console.WriteLine(httpResponse.StatusCode); Console.WriteLine(httpResponse.Method); Console.WriteLine(httpResponse.Headers); Stream st = httpResponse.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); Console.WriteLine(reader.ReadToEnd()); Console.WriteLine("\n");
}
public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }
----------------------------------------------
沈阳- |
作者: |
ozhy1 (ozhy1) |
★☆☆☆☆ |
-
|
普通会员 |
|
2017/8/13 20:35:24 |
1楼: |
//----------java----------
public static void main(String[] args) { String host = "https://ali-weather.showapi.com"; String path = "/phone-post-code-weeather"; String method = "GET"; String appcode = "你自己的AppCode"; Map<String, String> headers = new HashMap<String, String>(); //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 headers.put("Authorization", "APPCODE " + appcode); Map<String, String> querys = new HashMap<String, String>(); querys.put("need3HourForcast", "0"); querys.put("needAlarm", "0"); querys.put("needHourData", "0"); querys.put("needIndex", "0"); querys.put("needMoreDay", "0"); querys.put("phone_code", "021"); querys.put("post_code", "200000");
try { /** * 重要提示如下: * HttpUtils请从 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java * 下载 * * 相应的依赖请参照 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml */ HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys); System.out.println(response.toString()); //获取response的body //System.out.println(EntityUtils.toString(response.getEntity())); } catch (Exception e) { e.printStackTrace(); } }
----------------------------------------------
沈阳-
|
作者: |
ozhy1 (ozhy1) |
★☆☆☆☆ |
-
|
普通会员 |
|
2017/8/13 20:39:30 |
1楼: |
//----------c#---------
//using System.IO; //using System.Text; //using System.Net; //using System.Net.Security; //using System.Security.Cryptography.X509Certificates;
private const String host = "https://ali-weather.showapi.com"; private const String path = "/phone-post-code-weeather"; private const String method = "GET"; private const String appcode = "你自己的AppCode";
static void Main(string[] args) { String querys = "need3HourForcast=0&needAlarm=0&needHourData=0&needIndex=0&needMoreDay=0&phon_e_code=021&post_code=200000"; String bodys = ""; String url = host + path; HttpWebRequest httpRequest = null; HttpWebResponse httpResponse = null;
if (0 < querys.Length) { url = url + "?" + querys; }
if (host.Contains("https://")) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url)); } else { httpRequest = (HttpWebRequest)WebRequest.Create(url); } httpRequest.Method = method; httpRequest.Headers.Add("Authorization", "APPCODE " + appcode); if (0 < bodys.Length) { byte[] data = Encoding.UTF8.GetBytes(bodys); using (Stream stream = httpRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } } try { httpResponse = (HttpWebResponse)httpRequest.GetResponse(); } catch (WebException ex) { httpResponse = (HttpWebResponse)ex.Response; }
Console.WriteLine(httpResponse.StatusCode); Console.WriteLine(httpResponse.Method); Console.WriteLine(httpResponse.Headers); Stream st = httpResponse.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); Console.WriteLine(reader.ReadToEnd()); Console.WriteLine("\n");
}
public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }
----------------------------------------------
沈阳-
|
作者: |
|
2017/8/16 18:13:55 |
2楼: |
个人认为,java 转 delpgi 是最容易的。。。
----------------------------------------------
delphi 是兴趣,和工作无关,即使它倒闭。又不靠它 delphi 吃饭,怕甚?
|
作者: |
gmxyb (gmxyb) |
★☆☆☆☆ |
-
|
普通会员 |
|
2017/8/17 5:00:15 |
3楼: |
个人认为,随随便便就能找到 免费劳动力 是最不容易的。。。
----------------------------------------------
-
|
作者: |
|
2017/8/17 8:39:15 |
4楼: |
也许几天,也许几年,也许一辈子,楼主碰运气呢。
----------------------------------------------
(C)(P)Flying Wang
|
作者: |
drroc (mvcxe) |
★☆☆☆☆ |
-
|
盒子活跃会员 |
|
2017/8/23 11:04:30 |
5楼: |
其实就是简单的rest api请求,获取返回值,虽然没打算帮楼主写代码,不过也不是水贴,楼主可以看看delphi-rest-client-api的实现
https://github.com/fabriciocolombo/delphi-rest-client-api
顺便做个广告:也可以关注一下mvcxe,以后会提供一个微信公众号范例,到时里面有rest api调用的实现代码
----------------------------------------------
MVCXE中国首个DELPHI MVC WEB框架:https://www.mvcxe.com/
|
|