dongqu1783 2017-03-10 11:20 采纳率: 100%
浏览 108

从vb.net发布webrequest到php

i have a problem when using webrequest to pass to php

so i need to pass a parameter to php look like this

this is in php

public function get_token()
{
$client = new \GuzzleHttp\Client;

$requestdata = [
[
"name"=>'grant_type',
"contents" => 'client_credentials'
],
[
"name"=>'client_id',
"contents" => 'testclient'
],
[
"name"=>'client_secret',
"contents" => 'abcdefghijklmnopqrstuvwxyz12341234567890'
]
];

$response = $client->request('POST','http://abc123.local/authorizations', [
'multipart' => $requestdata
]);

$data = $response->getBody()->getContents();
$data = json_decode($data, TRUE);

$token = $data['token_type'].' '.$data['access_token'];

return $token;
}

and this is my code in vb.net

     Sub token()

Dim grand_type As String = "client_credentials"
Dim client_id As String = "testclient"
Dim client_secret As String = "abcdefghijklmnopqrstuvwxyz12341234567890"
Dim strHeaders As String
Dim urlAuth As String = "http://abc123.local/authorizations"

strHeaders = String.Format("grant_type={0}&client_id={1}&client_secret={2}",
grand_type, client_id, client_secret)

Dim JSONEncode As String
JSONEncode = JsonConvert.SerializeObject(strHeaders)

Dim byteData As Byte() = Encoding.UTF8.GetBytes(JSONEncode )
Dim httpReq As HttpWebRequest = TryCast(HttpWebRequest.Create(urlAuth), HttpWebRequest)
httpReq.Method = "POST"
httpReq.ContentType = "multipart/form-data"
httpReq.ContentLength = byteData.Length

'-=-=-=-=-=-=-=-= Sample Sending Data -=-=-=-=-=-=-=-
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
ServicePointManager.ServerCertificateValidationCallback =
New System.Net.Security.RemoteCertificateValidationCallback(AddressOf AcceptAllCertifications)

Dim reqStream As Stream = httpReq.GetRequestStream()
reqStream.Write(byteData, 0, byteData.Length)

Dim request As WebRequest = WebRequest.Create(urlAuth)
'-=-=-=-=-=-=-=-= Sample Receiving Data -=-=-=-=-=-=-=-=
Dim resStream As Stream = httpReq.GetResponse.GetResponseStream()  
Dim objReader As New StreamReader(resStream, Encoding.UTF8)
Dim wr As WebResponse = httpReq.GetResponse()
Dim receiveStream As Stream = wr.GetResponseStream()
Dim reader As New StreamReader(receiveStream, Encoding.UTF8)
Dim content As String = reader.ReadToEnd()
End Sub


Private Function AcceptAllCertifications(sender As Object, certification As System.Security.Cryptography.X509Certificates.X509Certificate,
chain As System.Security.Cryptography.X509Certificates.X509Chain, sslPolicyErrors As System.Net.Security.SslPolicyErrors) As Boolean
Return True
End Function

i got an error for bad request, for this part:

"Dim resStream As Stream = httpReq.GetResponse.GetResponseStream()"

any idea? thanks

  • 写回答

1条回答 默认 最新

  • dongwen4630 2017-03-12 03:38
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配