我正在寻求帮助,因为我尝试了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中的代码
结构Contactdata 公共姓氏为字符串 Public assigned_user_id As String 结束结构
尝试 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库的引用和Imported(Imports Newtonsoft.Json) 这不是问题,我确定我在使用params做错了。
有人可以帮我这个吗?
答案 0 :(得分:0)
这是工作代码
在模块中
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