locked
Json post request ( secure) error RRS feed

  • Question

  • User-1034672953 posted

    Hi
     

    i want to make a Json Post request in .net
    it must be secure
    i havce the .cer file , .pem file .pfx file the private key
    i wrote some code to that but the server on the other side say the request is missing the private key in it
    so plz help me in that i want this to be done
    i have HTTPs on my server i send also the .cer file to the client and they say they installed it

    i always gget this error ( bad request 400 bad requets)

    Sunday, February 15, 2015 10:22 AM

All replies

  • User2008642861 posted

    Hi Sami_Karaeen,

    Thank you for your post. Could you please provide us your code of request which could help us  to understand you more correctly.

    Without your code, I suggest that you could add a breakpoint at your code to check the details about why you got this error.

    Also, here is a similar case for you to refer.

    http://stackoverflow.com/questions/24730608/how-to-send-post-json-from-c-sharp-to-asp-net-web-api

    Hope this could be helpful to you.

    Best regards,

    Archer

    Monday, February 16, 2015 4:03 AM
  • User-1034672953 posted
    private void sendNotification()
        {//this is the method i use
            try
            {
                HttpWebRequest httpWReq =
                     (HttpWebRequest)WebRequest.Create("https://gw.bisan.com/api/apdemo_6/");
    
                Encoding encoding = new UTF8Encoding();
              string postData = @"{""user"":""XXXX"",""password"":""XXXXX"",""command"":""table"",""table"":""currency"",""fields"":[""symbol"",""rate""]}";
              
                byte[] data = encoding.GetBytes(postData);
    
               
                httpWReq.Method = "POST";
                httpWReq.ContentType = "text/json";//charset=UTF-8";
                
    
                var certificate = @"\Json3\pem\1.pfx";
                //var hostAddress = “hostAddress";
             
    
                X509Certificate2 certificatex = new X509Certificate2(Server.MapPath(certificate), "XXXXX");
                
               // this is a normal web form application not MVC
              
                
                httpWReq.ClientCertificates.Add(certificatex);
                httpWReq.ContentLength = data.Length;
                
    
                Stream stream = httpWReq.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Close();
    
                HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
                string s = response.ToString();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                String jsonresponse = "";
                String temp = null;
                while ((temp = reader.ReadLine()) != null)
                {
                    jsonresponse += temp;
                }
    
                Response.Write("Json Response : " + jsonresponse); 
    
            }
            catch (Exception ex) { Response.Write("Exception : " + ex.Message); }
    
        }

    this is the code i use and alwys getting 400 bad request

    Tuesday, February 17, 2015 12:36 AM
  • User2008642861 posted

    Hi Sami_Karaeen,

    When I want to reproduce your issue, I found that I was not clear about X509Certificate2. After I add a breakpoint at your code which it could run, I got the error at below code.

    HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();

    According to current information you provided, I suppose that your issue may caused by X509Certificate2. It could not be accessed or return a value. I am not sure my result I got could help you.

    Could you please debug your code by adding a breakpoint at the funciton you post, then press F10 to check where you got this error.

    Best regards,

    Archer

    Tuesday, February 17, 2015 3:54 AM
  • User-1034672953 posted

    hi 

    i use this code 

                X509Certificate2 certificatex = new X509Certificate2(Server.MapPath(certificate), "arvixe");

    couse the admin at the server told me that my request was missing the private key 

    even without it - i alwys have the error at this line 

    HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();

    The remote server returned an error: (400) Bad Request

    Tuesday, February 17, 2015 5:22 AM
  • User2008642861 posted

    Hi Sami_Karaeen,

    I suppose that httpWReq may not be accessed, please check the example I provide you above and try to revise your code. Also, I suggest that you could refer to the library at the MSDN, then re-write your code. With the information you provide, there is nothing incorrectly. I suggest that you could add a breakpoint at below code to check.

    Stream stream = httpWReq.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Close();

    https://msdn.microsoft.com/en-us/library/system.net.httpwebresponse(v=vs.110).aspx

    Best regards,

    Archer

    Sunday, February 22, 2015 12:17 AM