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


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

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

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


加好友 发短信
等级:六尾狐 帖子:1489 积分:10602 威望: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


加好友 发短信
等级:六尾狐 帖子:1489 积分:10602 威望: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

怎么改?

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


加好友 发短信
等级:六尾狐 帖子:1489 积分:10602 威望: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
确定



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


加好友 发短信
等级:六尾狐 帖子:1489 积分:10602 威望: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

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


加好友 发短信
等级:六尾狐 帖子:1489 积分:10602 威望:0 精华:0 注册:2018/10/16 11:42:00
  发帖心情 Post By:2021/1/11 10:54:00 [显示全部帖子]

MD5好像没找到有命名空间 

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


加好友 发短信
等级:六尾狐 帖子:1489 积分:10602 威望:0 精华:0 注册:2018/10/16 11:42:00
  发帖心情 Post By:2021/1/11 13:08:00 [显示全部帖子]

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

 回到顶部