Foxtable(狐表)用户栏目专家坐堂 → 【已解决】[求助]请问下这个中通api怎么写?


  共有3422人关注过本帖树形打印复制链接

主题:【已解决】[求助]请问下这个中通api怎么写?

帅哥哟,离线,有人找我吗?
zto001
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:六尾狐 帖子:1486 积分:10582 威望:0 精华:0 注册:2018/10/16 11:42:00
【已解决】[求助]请问下这个中通api怎么写?  发帖心情 Post By:2021/1/8 13:44:00 [只看该作者]

https://op.zto.cn/#/document?menuId=29


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var appKey = "d577e7b5024ad20446e10";
            var appSec = "0e8e9457d493666ee2f5adb783e69abb";

            var  js;
            using (HttpClient client = new HttpClient())
            {
                using (var content = new StringContent(jsonStr, Encoding.UTF8, "application/json"))
                {
                    content.Headers.Add("x-appKey", appKey);
                    var digest = GetSign(jsonStr, appKey, appSec);
                    content.Headers.Add("x-datadigest", digest);
                    HttpResponseMessage response = await client.PostAsync("https://japi-test.zto.com/zto.open.getOrderInfo", content);
                    response.EnsureSuccessStatusCode();
                    string responseBody = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(responseBody);
                }
            }
            Console.ReadLine();
        }

        public static String GetSign(String content,String appKey, String appSec)
        {
            String fullContent = content + appSec;

            byte[] buytes = Encoding.GetEncoding("UTF-8").GetBytes(fullContent);
            using (MD5 md5 = new MD5CryptoServiceProvider())
            {
                var outBytes = md5.ComputeHash(buytes);
                return Convert.ToBase64String(outBytes);
            }
        }
    }
}



还是一样,先用转换代码的转换是吧

http://converter.telerik.com/

[此贴子已经被作者于2021/1/11 15:29:02编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
zto001
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:六尾狐 帖子:1486 积分:10582 威望:0 精华:0 注册:2018/10/16 11:42:00
  发帖心情 Post By:2021/1/8 13:46:00 [只看该作者]

转换后


Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Net.Http
Imports System.Security.Cryptography
Imports System.Text
Imports System.Threading.Tasks

Namespace ConsoleApp1
    Class Program
        Private Shared Async Function Main(ByVal args As String()) As Task
            Dim appKey = "d577e7b5024ad20446e10"
            Dim appSec = "0e8e9457d493666ee2f5adb783e69abb"
            Dim js"type"":0,""orderCode"":""210107000003719103"",""billCode"":""""}"

            Using client As HttpClient = New HttpClient()

                Using content = New StringContent(jsonStr, Encoding.UTF8, "application/json")
                    content.Headers.Add("x-appKey", appKey)
                    Dim digest = GetSign(jsonStr, appKey, appSec)
                    content.Headers.Add("x-datadigest", digest)
                    Dim response As HttpResponseMessage = Await client.PostAsync("https://japi-test.zto.com/zto.open.getOrderInfo", content)
                    response.EnsureSuccessStatusCode()
                    Dim responseBody As String = Await response.Content.ReadAsStringAsync()
                    Console.WriteLine(responseBody)
                End Using
            End Using

            Console.ReadLine()
        End Function

        Public Shared Function GetSign(ByVal content As String, ByVal appKey As String, ByVal appSec As String) As String
            Dim fullContent As String = content & appSec
            Dim buytes As Byte() = Encoding.GetEncoding("UTF-8").GetBytes(fullContent)

            Using md5 As MD5 = New MD5CryptoServiceProvider()
                Dim outBytes = md5.ComputeHash(buytes)
                Return Convert.ToBase64String(outBytes)
            End Using
        End Function
    End Class
End Namespace



放到全局代码里面提示
-
编译错误:"Imports"语句前面必须是声明。
错误代码: Imports System

怎么改?

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110738 积分:563610 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2021/1/8 13:56:00 [只看该作者]

一样的处理方法
http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=159152&skin=0

 回到顶部
帅哥哟,离线,有人找我吗?
zto001
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:六尾狐 帖子:1486 积分:10582 威望:0 精华:0 注册:2018/10/16 11:42:00
  发帖心情 Post By:2021/1/8 14:18:00 [只看该作者]

Private Async Function Main(ByVal args As String()) As Task
Dim appKey = "d577e7b5024ad20446e10"
Dim appSec = "0e8e9457d493666ee2f5adb783e69abb"
Dim js"type"":0,""orderCode"":""210107000003719103"",""billCode"":""""}"
Using client As System.Net.Http.HttpClient= New System.Net.Http.HttpClient()
Using content = New StringContent(jsonStr, Encoding.UTF8, "application/json")
content.Headers.Add("x-appKey", appKey)
Dim digest = GetSign(jsonStr, appKey, appSec)
content.Headers.Add("x-datadigest", digest)
Dim response As HttpResponseMessage = Await client.PostAsync("https://japi-test.zto.com/zto.open.getOrderInfo", content)
response.EnsureSuccessStatusCode()
Dim responseBody As String = Await response.Content.ReadAsStringAsync()
Console.WriteLine(responseBody)
End Using
End Using

Console.ReadLine()
End Function

Public Function GetSign(ByVal content As String, ByVal appKey As String, ByVal appSec As String) As String
Dim fullContent As String = content & appSec
Dim buytes As Byte() = Encoding.GetEncoding("UTF-8").GetBytes(fullContent)

Using md5 As MD5 = New MD5CryptoServiceProvider()
Dim outBytes = md5.ComputeHash(buytes)
Return Convert.ToBase64String(outBytes)
End Using
End Function


是这样吗?去掉Shared 这个单词
命名空间HttpClient 改成System.Net.Http.HttpClient

提示
错误
x
编译错误:未定义类型"Task"。
错误代码:Private Function Main(Byval args As String0) As Task
确定



 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110738 积分:563610 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2021/1/8 14:44:00 [只看该作者]

这些是.net5.0的语法,foxtable用不了,咨询官方要支持.net 4.0的用法,或者试试

全局代码
Public Function GetSign(ByVal content As String, ByVal appKey As String, ByVal appSec As String) As String
Dim fullContent As String = content & appSec
Dim buytes As Byte() = Encoding.GetEncoding("UTF-8").GetBytes(fullContent)

Using md5 As MD5 = New MD5CryptoServiceProvider()
Dim outBytes = md5.ComputeHash(buytes)
Return Convert.ToBase64String(outBytes)
End Using
End Function

调用
Dim htc As new HttpClient("https://japi-test.zto.com/zto.open.getOrderInfo")
 Dim appKey = "d577e7b5024ad20446e10"
Dim appSec = "0e8e9457d493666ee2f5adb783e69abb"
htc
.Headers.Add("x-appKey", appKey)
dim digest = GetSign(jsonStr, appKey, appSec)
htc.Headers.Add("x-datadigest", digest)
htc.Content = "提交的json字符串"
htc.ContentType = "application/json"
dim res = htc.GetData()
msgbox(res)

 回到顶部
帅哥哟,离线,有人找我吗?
zto001
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:六尾狐 帖子:1486 积分:10582 威望:0 精华:0 注册:2018/10/16 11:42:00
  发帖心情 Post By:2021/1/8 16:20:00 [只看该作者]

错误
x
编译错误:未定义类型"MD5".
错误代码: Using md5 As MD5 = New MD5CryptoServiceProviderO
确定


全局代码
Public Function GetSign(ByVal content As String, ByVal appKey As String, ByVal appSec As String) As String
Dim fullContent As String = content & appSec
Dim buytes As Byte() = Encoding.GetEncoding("UTF-8").GetBytes(fullContent)

Using md5 As MD5 = New MD5CryptoServiceProvider()   这段错误
Dim outBytes = md5.ComputeHash(buytes)
Return Convert.ToBase64String(outBytes)
End Using
End Function

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  7楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110738 积分:563610 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2021/1/8 16:32:00 [只看该作者]

加上命名空间

 回到顶部
帅哥哟,离线,有人找我吗?
zto001
  8楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:六尾狐 帖子:1486 积分:10582 威望:0 精华:0 注册:2018/10/16 11:42:00
  发帖心情 Post By:2021/1/11 10:54:00 [只看该作者]

MD5好像没找到有命名空间 

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  9楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110738 积分:563610 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2021/1/11 11:09:00 [只看该作者]

肯定有。如果经常开发接口,建议安装一个vs,在vs项目里输入代码会有详细提示的。

转换后

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Net.Http
Imports System.Security.Cryptography
Imports System.Text
Imports System.Threading.Tasks

Using md5 As  System.Security.Cryptography.MD5CryptoServiceProvider= New System.Security.Cryptography.MD5CryptoServiceProvider()   这段错误

 回到顶部
帅哥哟,离线,有人找我吗?
zto001
  10楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:六尾狐 帖子:1486 积分:10582 威望:0 精华:0 注册:2018/10/16 11:42:00
  发帖心情 Post By:2021/1/11 13:08:00 [只看该作者]

jsonStr请教下这个值是什么值?json字符串吗?

 回到顶部
总数 11 1 2 下一页