2017-09-26 47 views
0

我正在嘗試使用POST方法在ASP.NET中以編程方式發出Web請求。 我想用web請求發送POST參數。類似這樣的:如何將對象作爲POST參數發送到ASP.Net Web請求?

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來獲得響應?

在此先感謝, Hemanth。

回答

1

您需要使用GetRequestStream()方法屬於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

感謝艾登回覆 request.ContentLength = objBytes.Length; Request.Contentlenght沒有獲取情報 – hemanth

+0

而在LoginData類.Ge​​tProperties中也沒有獲得智能 – hemanth

0

如果你想使用HttpClient

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」 的形式。使用「的NameValueCollection」您可以添加到發佈對象的數量 - 這種形式,你可以張貼到所需的URL的

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()。

相關問題