hsuwz
2017-12-31 14:13
采纳率: 75%
浏览 2.6k
已采纳

vb.net写的windows应用程序如何调用webapi?

我有个项目,需要在VB.NET写的WINDOWS应用程序中,利用WEBAPI按POST MULTI/FORM-DATA的形式向网站上传字符串和文件,不知如何写代码?我是个新手^0^

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • kafly 2017-12-31 14:21
    已采纳

    这个函数能帮到你

     Public Shared Function GetData(ByVal url As String, ByVal data As String) As String  
                Dim request As HttpWebRequest = WebRequest.Create(url + "?" + data)  
                request.Method = "GET"  
                Dim sr As StreamReader = New StreamReader(request.GetResponse().GetResponseStream)  
                Return sr.ReadToEnd  
        End Function 
    
    点赞 评论
  • kafly 2017-12-31 14:19

    WEB API 其实就是访问一个网页地址,参数一般通过URL方式提交,返回值根据该API的约定,或为JOSN或为XML,无非也就是一个字符串,解析一下就可以了。

    楼主现在是卡在不会访问网页还是不会组织URL,或者是解析返回值?

    点赞 评论
  • blownewbee 2017-12-31 15:22

    MULTI/FORM-DATA相当于上传文件了。
    参考下面的代码:

    Imports System  
    Imports System.Collections.Generic  
    Imports System.Text  
    Imports System.Net  
    Imports System.IO  
    Namespace SimpleWebRequest
    Class Program  
    static void Main(string() args)  
    {  
    Console.WriteLine( 
    PostDataToUrl("test", "http://www.microsoft.com")  
    )  
    }  
    '/<summary>  
    '/ Post data到url  
    '/</summary>  
    '/<param name="data">要post的数据</param>  
    '/<param name="url">目标url</param>  
    '/<returns>服务器响应</returns>  
    static String PostDataToUrl(String data, String url)  
    {  
    Dim encoding As Encoding =  Encoding.GetEncoding(sRequestEncoding) 
    Dim bytesToPost() As Byte =  encoding.GetBytes(data) 
    Return PostDataToUrl(bytesToPost,url)
    }  
    '/<summary>  
    '/ Post data到url  
    '/</summary>  
    '/<param name="data">要post的数据</param>  
    '/<param name="url">目标url</param>  
    '/<returns>服务器响应</returns>  
    static String PostDataToUrl(byte() data, String url)  
    {  
    #region 创建httpWebRequest对象  
    Dim webRequest As WebRequest =  WebRequest.Create(url) 
    Dim httpRequest As HttpWebRequest =  webRequest as HttpWebRequest 'as用于类型转换  
    If httpRequest Is Nothing Then
    Throw New ApplicationException( 
    String.Format("Invalid url string: {0}", url)  
    )
    End If
    #End Region  
    #region 填充httpWebRequest的基本信息  
    httpRequest.UserAgent = sUserAgent  
    httpRequest.ContentType = sContentType  
    httpRequest.Method = "POST"  
    #End Region  
    #region 填充要post的内容  
    httpRequest.ContentLength = data.Length  
    Dim requestStream As Stream =  httpRequest.GetRequestStream() 
    requestStream.Write(data, 0, data.Length)  
    requestStream.Close()  
    #End Region  
    #region 发送post请求到服务器并读取服务器返回信息  
    Dim responseStream As Stream
    Try
    responseStream = httpRequest.GetResponse().GetResponseStream()
    Catch e As Exception
    ' log error  
    Console.WriteLine( 
    String.Format("POST操作发生异常:{0}", e.Message)  
    )  
    Throw e
    End Try
    #End Region  
    #region 读取服务器返回信息  
    Dim stringResponse As String =  String.Empty 
    using(StreamReader responseReader =  
    New StreamReader(responseStream, Encoding.GetEncoding(sResponseEncoding)))  
    {  
    stringResponse = responseReader.ReadToEnd()  
    }  
    responseStream.Close()  
    #End Region  
    Return stringResponse
    End Namespace
    {  
    Class Program  
    static void Main(string() args)  
    {  
    Console.WriteLine( 
    PostDataToUrl("test", "http://www.microsoft.com")  
    )  
    }  
    '/<summary>  
    '/ Post data到url  
    '/</summary>  
    '/<param name="data">要post的数据</param>  
    '/<param name="url">目标url</param>  
    '/<returns>服务器响应</returns>  
    static String PostDataToUrl(String data, String url)  
    {  
    Dim encoding As Encoding =  Encoding.GetEncoding(sRequestEncoding) 
    Dim bytesToPost() As Byte =  encoding.GetBytes(data) 
    Return PostDataToUrl(bytesToPost,url)
    }  
    '/<summary>  
    '/ Post data到url  
    '/</summary>  
    '/<param name="data">要post的数据</param>  
    '/<param name="url">目标url</param>  
    '/<returns>服务器响应</returns>  
    static String PostDataToUrl(byte() data, String url)  
    {  
    #region 创建httpWebRequest对象  
    Dim webRequest As WebRequest =  WebRequest.Create(url) 
    Dim httpRequest As HttpWebRequest =  webRequest as HttpWebRequest 'as用于类型转换  
    If httpRequest Is Nothing Then
    Throw New ApplicationException( 
    String.Format("Invalid url string: {0}", url)  
    )
    End If
    #End Region  
    #region 填充httpWebRequest的基本信息  
    httpRequest.UserAgent = sUserAgent  
    httpRequest.ContentType = sContentType  
    httpRequest.Method = "POST"  
    #End Region  
    #region 填充要post的内容  
    httpRequest.ContentLength = data.Length  
    Dim requestStream As Stream =  httpRequest.GetRequestStream() 
    requestStream.Write(data, 0, data.Length)  
    requestStream.Close()  
    #End Region  
    #region 发送post请求到服务器并读取服务器返回信息  
    Dim responseStream As Stream
    Try
    responseStream = httpRequest.GetResponse().GetResponseStream()
    Catch e As Exception
    ' log error  
    Console.WriteLine( 
    String.Format("POST操作发生异常:{0}", e.Message)  
    )  
    Throw e
    End Try
    #End Region  
    #region 读取服务器返回信息  
    Dim stringResponse As String =  String.Empty 
    using(StreamReader responseReader =  
    New StreamReader(responseStream, Encoding.GetEncoding(sResponseEncoding)))  
    {  
    stringResponse = responseReader.ReadToEnd()  
    }  
    responseStream.Close()  
    #End Region  
    Return stringResponse
    }  
    const String sUserAgent =  
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"  
    const String sContentType =  
    "application/x-www-form-urlencoded"  
    const String sRequestEncoding = "ascii"  
    const String sResponseEncoding = "gb2312"
    End Class
    
    
    点赞 评论
  • hsuwz 2018-01-01 03:39

    @kafly
    非常感谢您的回复。问题的关键应该在如何组织URL访问网页

    对方给出的信息是这样的:
    接口地址:http://192.168.1.1/webapi (例)
    参数:paramters

    上传图片接口
    使用POST multipart/form-data形式上传,字段如下:
    字段名 类型
    project_id 字符串

    enterprise__uscc_ 字符串

    detecting_uscc 字符串

    image_id 字符串
    image_file 文件 图片文件内容

    我需要用vb.net 写一段代码,上传字串和文件内容

    点赞 评论
  • blueapple0750 2018-01-02 02:36

    http://www.cnblogs.com/developersupport/p/aspnet-webapi.html
    看看这篇文章,希望能帮助你

    点赞 评论

相关推荐 更多相似问题