[英]WebClient UploadString
have a problem. 有一个问题。
While post-processing with Web service I am getting error 使用Web服务进行后处理时出现错误
iqws webservis = new iqws();
WebClient wc = new WebClient();
var ser = new JavaScriptSerializer();
var serializedResult = ser.Serialize(webservis.getProducts());
wc.Headers[HttpRequestHeader.ContentType] = "application/json";
string result = wc.UploadString("http://localhost:3523/WS/iqws.asmx/getProducts", serializedResult);
var table = ser.Deserialize<Dictionary<string, dynamic>>(result);
But have error : The remote server returned an error: (500) Internal Server Error. 但是有错误: 远程服务器返回了错误:(500)Internal Server Error。
Why? 为什么?
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class iqws : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getProducts()
{
List<products> prd= new List<products>();
SqlConnection cn = new SqlConnection(ado.cnStr);
SqlCommand cmd = new SqlCommand("SELECT * FROM products", cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
products p = new products();
p.name = dr["name"].ToString();
p.money = dr["money"].ToString();
prd.Add(p);
}
var jsonSerialiser = new JavaScriptSerializer();
return jsonSerialiser.Serialize(prd);
}
}
And json post no problem.. 和json发布没问题..
$.ajax({
type: "POST",
url: "/WS/iqws.asmx/getProducts",
contentType: "application/json; charset=utf-8",
//data: {},
dataType: "json",
success: function (data) {
$("#jsonvalue").html(data.d);
},
error: function (xhr, status, error) {
$("#jsonvalue").html(xhr.responseText);
}
});
And result; 结果
[
{
"name": "iPhone 4s Gold",
"code": null,
"money": "1899,0000",
"images": null,
"comments": null
},
{
"name": "iPhone 5s Black",
"code": null,
"money": "2000,0000",
"images": null,
"comments": null
}
]
Why am I getting error? 为什么会出现错误?
While I question what you're trying to accomplish with your code, ultimately, the reason you are getting the HTTP 500
response is because you are POSTing data to a web service method that doesn't accept any parameters. 当我质疑您要用代码完成什么时,最终,得到
HTTP 500
响应的原因是因为您要将数据发布到不接受任何参数的Web服务方法中。
To call getProducts
with a POST, you need to call wc.UploadString(...)
with an empty string: 要使用POST调用
getProducts
,您需要使用空字符串调用wc.UploadString(...)
:
string result = wc.UploadString("http://localhost:3523/WS/iqws.asmx/getProducts", String.Empty);
That will retrieve the list of products. 这将检索产品列表。 To upload a product, you need a different web service method, eg:
要上传产品,您需要使用其他Web服务方法,例如:
public class Product
{
public String Name { get; set; }
public String Code { get; set; }
public String Money { get; set; }
public String Images { get; set; }
public String Comments { get; set; }
}
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class iqws : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getProducts()
{
List<Product> prd = new List<Product>();
//{
// new Product() { Name = "myname", Code = "mycode" },
// new Product() { Name = "myname2", Code = "mycode2" }
//};
using (SqlConnection cn = new SqlConnection(ado.cnStr))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Product", cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Product p = new Product();
p.Name = dr["name"].ToString();
p.Money = dr["money"].ToString();
prd.Add(p);
}
}
return new JavaScriptSerializer().Serialize(prd);
}
[WebMethod]
[ScriptMethod]
[GenerateScriptType(typeof(Product))]
public void addProduct(Product p)
{
// Place your validation code here to ensure the Product property values are in expected whitelists.
using (SqlConnection cn = new SqlConnection(ado.cnStr))
{
SqlCommand cmd = new SqlCommand("INSERT INTO Product (Name, Money) VALUES (@Name, @Money)", cn);
cmd.Parameters.AddWithValue("@Name", p.Name);
cmd.Parameters.AddWithValue("@Money", p.Money);
cn.Open();
cmd.ExecuteNonQuery();
}
}
}
And to query your service: 并查询您的服务:
var ser = new JavaScriptSerializer();
WebClient wc = new WebClient();
wc.Headers[HttpRequestHeader.ContentType] = "application/json";
string url = Request.Url.GetLeftPart(UriPartial.Authority) + "/WS/iqws.asmx/getProducts";
string json = wc.UploadString(url, String.Empty);
var data = ser.Deserialize<Dictionary<String, String>>(json);
List<Product> products = ser.Deserialize<List<Product>>(data["d"]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.