2017-09-26 9 views
0

POSTメソッドを使用してASP.NETでプログラムでWebリクエストを作成しようとしています。 私は、Webリクエストと共にPOSTパラメータも送信したいと思います。このようなもの:ASP.Net WebリクエストにPOSTパラメータとしてオブジェクトを送信する方法は?

LoginData obj = new LoginData(); 
obj.OSVersion = deviceInformation.OperatingSystem; 
obj.DeviceModel = deviceInformation.FriendlyName; 
string URI = "https://XXXXXXXXX.azure-mobile.net/user/logsuserin";   
HttpWebRequest GETRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(URI, UriKind.RelativeOrAbsolute)); 
GETRequest.Method = "POST"; 
GETRequest.ContentType = "application/x-www-form-urlencoded"; 
GETRequest.Headers["applicationKey"] = "UFakeKkrayuAeVnoVAcjY54545455544"; 
//GETRequest.Parameters.add(obj); 

もちろん、コメント行は機能しません。これをどのように達成するのですか?

私のobjをparamsとして送信することで、どのように応答するのですか?

ありがとうございます。 ヘマンツです。

答えて

1

あなたがHttpClientを使用したい場合はHttpWebRequest

void Main() 
{ 
    LoginData obj = new LoginData 
    { 
     Username = "foo", 
     Password = "Bar" 
    }; 

    byte[] objBytes = Encoding.UTF8.GetBytes(obj.ToString()); 

// obj.OSVersion = deviceInformation.OperatingSystem; 
// obj.DeviceModel = deviceInformation.FriendlyName; 
    string URI = "https://XXXXXXXXX.azure-mobile.net/user/logsuserin"; 
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(URI, UriKind.RelativeOrAbsolute)); 
    request.Method = "POST"; 
    request.ContentType = "application/x-www-form-urlencoded"; 
    request.Headers["applicationKey"] = "UFakeKkrayuAeVnoVAcjY54545455544"; 
    request.ContentLength = objBytes.Length; 

    using (Stream stream = request.GetRequestStream()) 
    { 
     stream.Write(objBytes, 0, objBytes.Length); 
    } 

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
    using (Stream stream = response.GetResponseStream()) 
    using (StreamReader reader = new StreamReader(stream)) 
    { 
     Console.WriteLine(reader.ReadToEnd()); 
    } 

} 

public class LoginData 
{ 
    public string Username { get; set; } 
    public string Password { get; set; } 
    public string OSVersion { get; set; } 
    public string DeviceModel { get; set; } 
    public override string ToString() 
    { 
     var temp = this.GetType() 
         .GetProperties() 
         .Select(p => $"{p.Name}={HttpUtility.UrlEncode(p.GetValue(this).ToString())}"); 

     return string.Join("&", temp); 
    } 
} 
+0

ありがとうございます。返信用Aydin request.ContentLength = objBytes.Length; Request.Contentlenghtがインテリジェンスを取得しない – hemanth

+0

LoginDataクラスで.GetPropertiesもインテリジェンスを取得しない – hemanth

0

に属するGetRequestStream()メソッドを使用する必要があります。

using (var client = new HttpClient()) 
{ 
    var request = new HttpRequestMessage(HttpMethod.Post, "https://XXXXXXXXX.azure-mobile.net/user/logsuserin"); 

    request.Headers.Add("applikationKey", "UFakeKkrayuAeVnoVAcjY54545455544"); 
    request.Content = new FormUrlEncodedContent(new[] 
    { 
     new KeyValuePair<string, string>("OSVersion", deviceInformation.OperatingSystem), 
     new KeyValuePair<string, string>("DeviceModel", deviceInformation.FriendlyName), 
    }); 

    var response = client.SendAsync(request).GetAwaiter().GetResult(); 
} 
0

あなたが動的に "NameValueCollectionの" とのFORMを生成することができます。

NameValueCollection FormFields = new NameValueCollection(); 
     FormFields.Add("abc", obj1); 
     FormFields.Add("xyz", obj2); 
    Response.Clear(); 
    Response.Write("<html><head>"); 
    Response.Write(string.Format("</head><body onload=\"document.{0}.submit()\">", FormName)); 
    Response.Write(string.Format("<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >", FormName, Method, Url)); 
    for (int i = 0; i < FormFields.Keys.Count; i++) 
    { 
     Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", FormFields.Keys[i], FormFields[FormFields.Keys[i]])); 
    } 
    Response.Write("</form>"); 
    Response.Write("</body></html>"); 
    Response.End(); 

のOnLoad()希望のURLを投稿することができ、この形の - 「NameValueCollectionの」あなたのように掲載されるオブジェクトの数を追加することができますを使用します。

関連する問題