如何使用webmethod将HTML代码更改为C# [英] How can I change HTML code to C# using webmethod
本文介绍了如何使用webmethod将HTML代码更改为C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< form method =postaction =http://test.co.in/api/bus/search_trip.php>
< textarea name =request>
{uid:XXXX,pin:XXXXX,source_id:1270,destination_id:323,日期:25-04-2017}
< / textarea>
<! - < input type =textname =valuevalue =json> - >
< input type =submitname =submitvalue =submit>
< / form>
我尝试了什么:
当我使用ASP.NET C#向给定网址发出请求时,它只返回无效请求格式
解决方案
< pre lang =c#> string json = { \uid \:\xxxx \,\pin \:\xxxx \,\source_id \:\1270 \,\ destination_id \:\323 \,\date \:\05-05-2017 \};
var httpContent = new StringContent(json,Encoding.UTF8, application / json);
使用( var httpClient = new HttpClient())
{
// 此处出错
var httpResponse = await httpClient.PostAsync( http://pudhukadai.co.in/api/bus/search_trip.php,httpContent);
if (httpResponse.Content!= null )
{
// 此处出错
var responseContent = await httpResponse.Content.ReadAsStringAsync();
}
}
你可能想看看Newtonsoft.Json来处理创建Json字符串。
<form method="post" action="http://test.co.in/api/bus/search_trip.php"> <textarea name="request"> { "uid" : "XXXX", "pin":"XXXXX", "source_id":"1270", "destination_id":"323", "date":"25-04-2017" } </textarea> <!--<input type="text" name="value" value="json">--> <input type="submit" name="submit" value="submit"> </form>
What I have tried:
when i m giving the request to the given url using ASP.NET C# it returns only "Invalid Request Format"
解决方案
string json = "{\"uid\": \"xxxx\",\"pin\": \"xxxx\",\"source_id\": \"1270\",\"destination_id\": \"323\",\"date\": \"05-05-2017\"}"; var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); using (var httpClient = new HttpClient()) { // Error here var httpResponse = await httpClient.PostAsync("http://pudhukadai.co.in/api/bus/search_trip.php", httpContent); if (httpResponse.Content != null) { // Error Here var responseContent = await httpResponse.Content.ReadAsStringAsync(); } }
and you may want to look at Newtonsoft.Json to handle creating the Json string.
这篇关于如何使用webmethod将HTML代码更改为C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文