PUT请求在vb.net中工作但不在C#.net中工作

时间:2017-02-09 12:04:37

标签: c# .net json vb.net http-put

我正在使用C#开发一个REST客户端,它执行以下操作:

  1. 它向服务器发送一个POST请求,其中包含查询字符串参数PolicyId = myhotp和此json数据{" uid":" name "}
  2. 服务器使用状态为id的json数据进行响应{" state":" 323434j2hj"}
  3. 我提取" state"的值从收到的json数据中发送 它在另一个PUT请求中使用OTP向同一服务器进行验证。 stateid作为查询字符串参数StateId = ihhfueh8f88发送,otp作为json数据发送{" otp":" 123456"}。服务器根据状态ID验证OTP。
  4. 但是服务器响应了stateid无效。具有讽刺意味的是,当我使用旧的方式(即Microsoft.XMLHTTP来发出请求)时,同样的过程也可以工作,但它不能与HTTP客户端一起使用。我无法在查询字符串中传输状态ID。 这是我使用HTTP客户端的代码:

            User user = new User();
            user.uid = "username";
            String json = JsonConvert.SerializeObject(user);
            MessageBox.Show(json);
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://fqdn");
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            var response = client.PostAsync("api?myhotp", new StringContent(json, Encoding.UTF8, "application/json")).Result;
            var contents = response.Content.ReadAsStringAsync().Result;
            MessageBox.Show(contents);
            var obj = JObject.Parse(contents);
            string id = (string)obj["state"];
            MessageBox.Show(id);
            Otp otpObj = new Otp();
            otpObj.otp = "123456";
            String jsonotp = JsonConvert.SerializeObject(otpObj);
            MessageBox.Show(jsonotp);
            client = new HttpClient();
            client.BaseAddress = new Uri("http://fqdn");
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            response = client.PutAsync("api?StateId="+id, new StringContent(json, Encoding.UTF8, "application/json")).Result;
            contents = response.Content.ReadAsStringAsync().Result;
            MessageBox.Show(contents);
    

    以下是Microsoft.XMLHTTP的代码:

           Type xmlType = Type.GetTypeFromProgID("Microsoft.XMLHTTP");
            dynamic objXML = Activator.CreateInstance(xmlType);
            objXML.Open("POST", "http://fqdn/api?PolicyId=myhotp", false);
            objXML.setRequestHeader("Content-Type", "application/json");
            objXML.setRequestHeader("Accept", "application/json");
            //objXML.setOption(2, 13056);
            objXML.Send(json);
            MessageBox.Show(objXML.status.ToString());
            MessageBox.Show(objXML.responseText.ToString());
            var obj = JObject.Parse(objXML.responseText.ToString());
            String id = (String)obj["state"];
            MessageBox.Show(id);
            Otp otpObj = new Otp();
            otpObj.otp = "123456";
            json = JsonConvert.SerializeObject(otpObj);
            MessageBox.Show(json);
            objXML.Open("PUT", "http://fqdn/api?StateId="+id, false);
            objXML.setRequestHeader("Content-Type", "application/json");
            objXML.setRequestHeader("Accept", "application/json");
            objXML.Send(json);
            MessageBox.Show(objXML.status.ToString());
            MessageBox.Show(objXML.responseText.ToString());
    

0 个答案:

没有答案