【问题标题】:Translate PHP Json Webservice request TO VB.Net Json Webservice request将 PHP Json Webservice 请求转换为 VB.Net Json Webservice 请求
【发布时间】:2011-06-13 01:00:40
【问题描述】:

我正在寻求帮助,因为我尝试了 1000 个解决方案,但没有人与我合作,可能是因为我不太了解 JSON 和 PHP。 我必须向 Webservice 发送请求,并且我有一个有效的 PHP 解决方案,我必须在 VB.net 中进行翻译

这是在 PHP 中工作的代码

//fill in the details of the contacts.userId is obtained from loginResult.
$contactData  = array('lastname'=>'Valiant', 'assigned_user_id'=>$userId);
//encode the object in JSON format to communicate with the server.
$objectJson = Zend_JSON::encode($contactData);
//name of the module for which the entry has to be created.
$moduleName = 'Contacts';

//sessionId is obtained from loginResult.
$params = array("sessionName"=>$sessionId, "operation"=>'create', 
    "element"=>$objectJson, "elementType"=>$moduleName);
//Create must be POST Request.
$httpc->post("$endpointUrl", $params, true);
$response = $httpc->currentResponse();
//decode the json encode response from the server.
$jsonResponse = Zend_JSON::decode($response['body']);

//operation was successful get the token from the reponse.
if($jsonResponse['success']==false)
    //handle the failure case.
    die('create failed:'.$jsonResponse['error']['errorMsg']);
$savedObject = $jsonResponse['result'];
$id = $savedObject['id'];

好的,这是我在 VB.net 中的代码

结构联系数据 公开姓氏作为字符串 公共assigned_user_id 作为字符串 末端结构

试试 Dim uri As New Uri("webservice.php")

        Dim contactdata As New Contactdata
        contactdata.lastname = "Valiant"
        contactdata.assigned_user_id = "19x1"

        Dim objectJson As String = JavaScriptConvert.SerializeObject(contactdata)

        Dim moduleName As String = "Contacts"

        Dim params As String() = {"sessionName=" & session, "operation=create", "element=" & objectJson, "elementType=" & moduleName}

        Dim request As HttpWebRequest = HttpWebRequest.Create(uri)
        request.Method = WebRequestMethods.Http.Post
        request.ContentLength = params.Length
        request.ContentType = "application/x-www-form-urlencoded"

        Dim writer As New StreamWriter(request.GetRequestStream)
        writer.Write(params)
        writer.Close()
        Dim oResponse As HttpWebResponse = request.GetResponse()
        Dim reader As New StreamReader(oResponse.GetResponseStream())
        Dim tmp As String = reader.ReadToEnd()
        oResponse.Close()


    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

我自然地导入了对 Json 库的引用并导入了(导入 Newtonsoft.Json) 这不是问题,我确定我的参数有问题。

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 我对 vb 不是很熟悉,但我确实了解您在做什么。你遇到了什么问题?
  • 问题是,如果我在没有 Json 的情况下发送这个请求,响应是他找不到任何“lastname”字段,这很正常,因为他希望 lastname 和 assignment_user_id 为 Json 对象格式
  • 在我写的这段代码中,我在webservice响应之前收到一个错误,应用程序没有到达那个点,当他到达这个点时它返回一个错误code writer.Close()这让我甚至在没有 JSON 的情况下尝试之前,当“参数”不正确时,它就出现了,所以他不能写它,所以他也不能关闭写
  • 应该这样: Dim params As String() = {"sessionName=" & session, "operation=create", "element=" & objectJson, "elementType=" & moduleName} 是这样的: Dim params As String = "sessionName=" & session & "&operation=create" & "&element=" & objectJson & "&elementType=" & moduleName 还是自动发生?
  • 是的,您也可以按照您的建议进行操作,这是一个唯一的字符串,我也尝试过在最后一个代码之前,使用“code Sim params As String”的区别在于 params 不仅仅是 1 string,但是变成了字符串数组,为params(0) = "sessionName=" & session", params(1) = "operation=create",但是结果一定是一样的,问题一定出在别的地方.. :(

标签: php vb.net web-services json http-post


【解决方案1】:

这是工作代码

在一个模块中

Structure Contactdata
Public lastname As String
Public assigned_user_id As String
End Structure

然后在我的表单中

Imports Newtonsoft.Json



Dim contactdata As New Contactdata
contactdata.lastname = "Valiant"
contactdata.assigned_user_id = "19x1"

Dim objectJson As String = JavaScriptConvert.SerializeObject(contactdata)

Dim moduleName As String = "Contacts"

Dim params As String = "sessionName=" & session & "&operation=create" & "&element=" & objectJson & "&elementType=" & moduleName

Dim request As HttpWebRequest = HttpWebRequest.Create(uri)
request.Method = WebRequestMethods.Http.Post
request.ContentLength = params.Length
request.ContentType = "application/x-www-form-urlencoded"

Dim writer As New StreamWriter(request.GetRequestStream)
writer.Write(params)
writer.Close()
Dim oResponse As HttpWebResponse = request.GetResponse()
Dim reader As New StreamReader(oResponse.GetResponseStream())
Dim tmp As String = reader.ReadToEnd()
oResponse.Close()


Catch ex As Exception
MsgBox(ex.ToString)
End Try

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 2015-06-01
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多