如何调用API

wackyboy 2013-01-14 02:36:29
请问,如何通过控制台程序调用别的公司的API?有URL,可有KEY
...全文
391 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
viki117 2013-01-21
  • 打赏
  • 举报
回复
引用 11 楼 wknight_IT 的回复:
引用 7 楼 wackyboy 的回复:对方提供的实例为Java的 con.setRequestProperty("Content-type", "text/html"); con.setRequestProperty("Accept-Charset", "GBK"); con.setRequestProperty("contentType", "GBK"); ……
你不要去纠结是java还是C#,因为HTTP通信是通用的,只是java和C#定义http的语法有点区别,都是定义头文件和内容,上面那段JAVA大概意思是text/html格式,GBK地编码,appkey这个有点怪怪的方法 C# 实现的代码应该是这样 request.ContentType = "text/html"; request.Headers["Content-type"] = "text/html"; request.Headers["Accept-Charset"] = "GBK"; request.Headers["contentType"] = "GBK"; request.Headers["appkey"] = (string) map.get("appkey"); 然后再给request写入内容的时候用GBK编码 Encoding encode = System.Text.Encoding.GetEncoding("GBK"); 开作数据流的处理
wackyboy 2013-01-21
  • 打赏
  • 举报
回复
引用 14 楼 viki117 的回复:
引用 11 楼 wknight_IT 的回复: 引用 7 楼 wackyboy 的回复:对方提供的实例为Java的 con.setRequestProperty("Content-type", "text/html"); con.setRequestProperty("Accept-Charset", "GBK"); con.setRequestProperty("contentTyp……
可能是对方的API的原因吧,我将Request的内容用流处理,并不成功,虽然他们给的Java的实例也是流操作的。 现在结果是将Request内容跟其他的一样,放到Request.Headers中就可以了。 感谢以上大虾们的帮助。
风骑士之怒 2013-01-18
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/zh-cn/library/system.net.httpwebrequest%28v=VS.80%29.aspx
holon2013 2013-01-18
  • 打赏
  • 举报
回复
风骑士之怒 2013-01-18
  • 打赏
  • 举报
回复
引用 7 楼 wackyboy 的回复:
对方提供的实例为Java的 con.setRequestProperty("Content-type", "text/html"); con.setRequestProperty("Accept-Charset", "GBK"); con.setRequestProperty("contentType", "GBK"); con.setRequestPrope……
不就是类似HttpWebRequest的运用吗,设置头信息,
EnForGrass 2013-01-18
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
路过学习下,混个脸熟
taylor_fjzhtaobao 2013-01-18
  • 打赏
  • 举报
回复
我很想知道怎么学习C#
wackyboy 2013-01-17
  • 打赏
  • 举报
回复
对方提供的实例为Java的 con.setRequestProperty("Content-type", "text/html"); con.setRequestProperty("Accept-Charset", "GBK"); con.setRequestProperty("contentType", "GBK"); con.setRequestProperty("appkey", (String) map.get("appkey")); 这种 con 为 HttpURLConnection 这个 setRequestProperty 在C# 中怎么弄? 我看 HttpWebRequest 也没找到类似的功能 谢谢
  • 打赏
  • 举报
回复
一般url里都有key,直接拼接url请求源代码就行 http://abc.com?id=1//id就是key
codinghello 2013-01-16
  • 打赏
  • 举报
回复
一般的api都有文档的,看文档定义的格式。 一般都是HttpWebRequest来实现的。
wackyboy 2013-01-16
  • 打赏
  • 举报
回复
content的XML格式是API提供方设定的格式吧?
引用 3 楼 viki117 的回复:
C# code ? 123456789101112131415161718192021222324252627282930313233343536373839404142434445 /// <summary> /// 渠道实时状态获取 /// </summary> /// <param name="url">地址</para……
viki117 2013-01-14
  • 打赏
  • 举报
回复

 /// <summary>
        /// 渠道实时状态获取
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="requestStr">请求http包体内容</param>
        /// <param name="responeStr">回执http包体内容</param>
        internal void Request_Http(string url, string requestStr, ref string responeStr)
        {
            try
            {
                //生成http请求
                HttpWebRequest request = createRequest(requestStr, url, 5 * 1000);
                //获取回执
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                responeStr = getResponseContent(response);
            }
            catch (Exception ex)
            {
                logger.error("<" + msg.mMessageID + "> exception:{0}", ex.Message);
            }
        }

        /// <summary>
        /// 创建请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="xmlMessage">内容(无则string.Empty)</param>
        /// <param name="timeout">请求超时时间,单位:毫秒</param>
        /// <returns>请求对象</returns>
        private static HttpWebRequest createRequest(string xmlMessage, string url, int timeout)
        {
            Uri uri = new Uri(url);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            byte[] arrB = encode.GetBytes("content=" + xmlMessage);
            request.Timeout = timeout;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = arrB.Length;
            Stream outStream = request.GetRequestStream();
            outStream.Write(arrB, 0, arrB.Length);
            outStream.Close();
            return request;
        }
viki117 2013-01-14
  • 打赏
  • 举报
回复
URL提供的话,一般用http的方式来实现,HttpWebRequest和HttpWebResponse2个类来实现, 请求一般有2种,一种直接通过url带参数来实现,比如在url中就包含了key,一种是Content(http包体)来实现参数的传递,这个要看你API提供者使用的是那种方式; 回执一般就是返回http包体来实现的,这个包体的内容可以是任何字符串,xml,json字符串居多,好解析嘛.
真相重于对错 2013-01-14
  • 打赏
  • 举报
回复
想想webservice

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧