System.Net.Http.HttpRequestException with UnauthorizedException - c#

I am trying to convert my Python code into C# code but I got some exceptions during the translation.
What I want is to fetch json/string data from the url. I am new to C# network programming. I tried several ways, read there documents, google their usages, but I still couldn't find out the correct way, as I keep getting the exceptions in the title.
This is my Python code that works:
url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp'
params = {
'ct': 24,
'qqmusic_ver': 1298,
'new_json': 1,
'remoteplace':'sizer.yqq.lyric_next',
'searchid': 63514736641951294,
'aggr': 1,
'cr': 1,
'catZhida': 1,
'lossless': 0,
'sem': 1,
't': 7,
'p': 1,
'n': 1,
'w': keyword,
'g_tk': 1714057807,
'loginUin': 0,
'hostUin': 0,
'format': 'json',
'inCharset': 'utf8',
'outCharset': 'utf-8',
'notice': 0,
'platform': 'yqq.json',
'needNewCode': 0
}
headers = {
'content-type': 'application/json',
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0',
'referer':'https://y.qq.com/portal/search.html'
}
result = requests.get(url, params = params, headers = headers)
This is the C# code that I have tried:
public static async Task<string> SearchLyrics(string keyword)
{
keyword = Uri.EscapeUriString(keyword);
// method 1
string uri = $"https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=sizer.yqq.lyric_next&searchid=63514736641951294&aggr=1&cr=1&catZhida=1&lossless=0&sem=1&t=7&p=1&n=1&w={keyword}&g_tk=1714057807&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0";
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(uri);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (var request = new HttpRequestMessage(HttpMethod.Get, uri))
{
request.Headers.Add("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0");
request.Headers.Add("referer", "https://y.qq.com/portal/search.html");
var response = await client.SendAsync(request);
//response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
return await response.Content.ReadAsStringAsync();
}
}
// method 2
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("ct", "24");
client.DefaultRequestHeaders.Add("qqmusic_ver", "1298");
client.DefaultRequestHeaders.Add("new_json", "1");
client.DefaultRequestHeaders.Add("remoteplace", "sizer.yqq.lyric_next");
client.DefaultRequestHeaders.Add("searchid", "63514736641951294");
client.DefaultRequestHeaders.Add("aggr", "1");
client.DefaultRequestHeaders.Add("catZhida", "1");
client.DefaultRequestHeaders.Add("lossless", "0");
client.DefaultRequestHeaders.Add("t", "7");
client.DefaultRequestHeaders.Add("p", "1");
client.DefaultRequestHeaders.Add("n", "1");
client.DefaultRequestHeaders.Add("w", keyword);
client.DefaultRequestHeaders.Add("g_tk", "1714057807");
client.DefaultRequestHeaders.Add("loginUin", "0");
client.DefaultRequestHeaders.Add("hostUin", "0");
client.DefaultRequestHeaders.Add("format", "json");
client.DefaultRequestHeaders.Add("inCharset", "utf8");
client.DefaultRequestHeaders.Add("outCharset", "utf-8");
client.DefaultRequestHeaders.Add("notice", "0");
client.DefaultRequestHeaders.Add("platform", "yqq.json");
client.DefaultRequestHeaders.Add("needNewCode", "0");
using (var request = new HttpRequestMessage(HttpMethod.Get, "https://c.y.qq.com/soso/fcgi-bin/client_search_cp"))
{
request.Headers.Add("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0");
request.Headers.Add("referer", "https://y.qq.com/portal/search.html");
var response = await client.SendAsync(request);
//response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
return await response.Content.ReadAsStringAsync();
}
}
}

The problem is that I didn't enable network access ability for my uwp app. Adding it in the Package.appxmanifest would solve this issue.
My code is right but I have made some improvements according to the Microsoft Document:
public static async Task<string> SearchLyrics(string keyword)
{
string uri = $"https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=sizer.yqq.lyric_next&searchid=63514736641951294&aggr=1&cr=1&catZhida=1&lossless=0&sem=1&t=7&p=1&n=1&w={Uri.EscapeUriString(keyword)}&g_tk=1714057807&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.UserAgent.TryParseAdd("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0");
var response = await client.GetAsync(uri);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
Windows.Data.Json.JsonObject json = Windows.Data.Json.JsonObject.Parse(content);
return json.GetNamedObject("data").GetNamedObject("lyric").GetNamedArray("list").GetObjectAt(0).GetNamedString("content");
}
}

Related

HttpClient Post data form-url-encoded (national encoding - win-1250) [duplicate]

I'm using the HttpClient. I'm posting with web form parameters. One of the values (not name) is a foreign Swedish character ö , #246; ö ASCII: Latin Small Letter O Umlaut
Manually, IE, Firefox and Chrome all convert this character to S%F6k and everything works fine. However VS 2012 C# release converts it (via FormUrlEncodedContent(dict)) to %C3%B6
Is there a way to tell VS 2012 to convert it, to the friendly S%F6k (and still use HttpClient)?
I've attached most of the code, which may help others (cookies, proxy, etc...)
// Create Handler
var handler = new HttpClientHandler();
// Cookies
var cc = new CookieContainer();
handler.CookieContainer = cc;
// Proxy - for fiddler
WebProxy proxy = new WebProxy();
proxy.Address = new Uri("http://localhost:8888");
handler.Proxy = proxy;
// Create the client
var client = new HttpClient(handler);
var request4 = new HttpRequestMessage();
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("Accept", "text/html, application/xhtml+xml, */*");
client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");
client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.8,sv-SE;q=0.5,sv;q=0.3");
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
// Form Data
var dict4 = new Dictionary<string, string>
{
{ "page", "kantlista" },
{ "kod", "A0004n" },
{ "termin", "H12" },
{ "anmkod", "17113" },
{ "urval", "ant" },
{ "listVal", "namn" },
{ "method", "Sök" } // S%F6k
}; // dict
request4.Content = new FormUrlEncodedContent(dict4);
var value4 = new FormUrlEncodedContent(dict4);
string uri4 = "https://www.ltu.se/ideal/ListaKursant.do";
var response4 = await client.PostAsync(uri4, value4);
response4.Headers.Add("Cache-Control", "no-cache")
response4.EnsureSuccessStatusCode();
string responseBody4 = await response4.Content.ReadAsStringAsync();
FormUrlEncodedContent class encode form data in utf8 encoding.
try ByteArrayContent class and HttpUtility.UrlEncode(String, Encoding) to encode.
Just to complete #TylerTsai's answer
Replace
var dict = new Dictionary<string, string>();
dict.Add("param1", value1);
dict.Add("param1", value2);
var response = await httpClient.PostAsync(endpoint, new FormUrlEncodedContent(dict));
With
string postData = HttpUtility.UrlEncode(
$"param1={value1}&param2={value2}",Encoding.GetEncoding(myEncoding));
byte[] data = System.Text.Encoding.GetEncoding(myEncoding).GetBytes(postData);
ByteArrayContent content = new ByteArrayContent(data);
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
var response = await httpClient.PostAsync(endpoint, content);
With this small adjustment you can keep your dictionary
var postEncoding= "ISO-8859-1";
var dict4 = new Dictionary<string, string>
{
{ "page", "kantlista" },
{ "kod", "A0004n" },
{ "termin", "H12" },
{ "anmkod", "17113" },
{ "urval", "ant" },
{ "listVal", "namn" },
{ "method", "Sök" } // S%F6k
}; // dict
string postData = HttpUtility.UrlEncode(string.Join("&", dict4.Select(kvp => $"{kvp.Key}={kvp.Value}")) ,Encoding.GetEncoding(postEncoding));
byte[] data = Encoding.GetEncoding(postEncoding).GetBytes(postData);
ByteArrayContent content = new ByteArrayContent(data);
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
var response = await httpClient.PostAsync(endpoint, content);

Some specials characters are wrong in response of post request

I am trying to make a post request and receive a pdf text, but the response comes with multiple "�", that makes pdf only a white page.
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("Host", "bankline.itau.com.br");
client.DefaultRequestHeaders.Add("accept", "application/json");
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0");
client.DefaultRequestHeaders.Add("Accept-Language", "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3");
client.DefaultRequestHeaders.Add("Referer", "https://ww2.itau.com.br/2viabloq/blqpesquisado.asp");
client.DefaultRequestHeaders.Add("Origin", "https://ww2.itau.com.br");
client.DefaultRequestHeaders.Add("Connection", "keep-alive");
client.DefaultRequestHeaders.Add("Cookie", "*****************");
client.DefaultRequestHeaders.Add("Upgrade-Insecure-Requests", "1");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var values4 = new Dictionary<string, string>
{
{ "id", idPost },
{ "op", opPost }
};
var content4 = new FormUrlEncodedContent(values4);
var response4 = await client.PostAsync(url5, content4);
var buffer = await response4.Content.ReadAsByteArrayAsync();
var byteArray = buffer.ToArray();
var responseString4 = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
return responseString4;
This is making a post request in a url that is normally reached by normal browser access and it is working fine, but when I do this post request it returns this wrong special character multiple times in text.
Example of what I get:
in the first lines:
%PDF-1.4
%����
in the middle of document:
stream
H��W[o�6~����C)�)]G�s��n_�=02����T������e{��$q��c~���(L��
but some parts are correct:
/XObject <</Im0 10 0 R
>>
/ProcSet [/PDF
/Text
/ImageC
/ImageI

API error 401 Unauthorized laravel C Sharp

I am developing an application using C # that calls a RESTApi de laravel. Through Postman it works correctly, but instead of C# no, it returns error 401.
If I remove the following if in the Laravel controller it works fine:
if ($ request-> isJson ()) {
The header Content-Type is set to application/json
Laravel Code
function getResult(Request $request, $id)
{
if ($request->isJson()) {
// Eloquent
$times = Result::selectRaw('THE SELECT')
->where('ID', $Id)
->get();
$result = [];
foreach($times as $key => $time)
{
...........
}
sort($result);
return response()->json(['results'=>$result], 200);
}
return response()->json(['error' => 'Unauthorized'], 401, []);
}
C#
public static async Task<dynamic> GETTimes(int eventID, int stageID)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(baseUrl);
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
client.DefaultRequestHeaders.TryAddWithoutValidation("cache-control", "no-cache");
client.DefaultRequestHeaders.Add("User-Agent", #"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36");
// Add the Authorization header with the AccessToken.
//client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
// create the URL string.
string url = string.Format("api/v1/events/{0}/results/{1}", eventID, stageID);
// make the request
HttpResponseMessage response = await client.GetAsync(url);
// parse the response and return the data.
string jsonString = await response.Content.ReadAsStringAsync();
object responseData = JsonConvert.DeserializeObject(jsonString);
return (dynamic)responseData;
}
}

C# HttpClient + Cookiecontainer works on localhost fails on production

I built a function that its purpose is to emulate a real browser's client surfing a website, submitting a form and receiving a result from that website.
Here is my code:
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler))
{
handler.Proxy = new WebProxy("127.0.0.1", 8888); //Allow Fiddler
var response = client.PostAsync(baseAddress, null).Result;
var cookies = response.Headers.GetValues("Set-Cookie").ToList();
var sessionCookie = cookies[0];
sessionCookie = sessionCookie.Split(';')[0];
var sessionId = sessionCookie.Replace("ASP.NET_SessionId=", "");
cookieContainer.Add(baseAddress, new Cookie("ASP.NET_SessionId", sessionId, "/"));
client.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36");
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
client.DefaultRequestHeaders.Add("Referer", "http://");
client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.8,he;q=0.6");
client.DefaultRequestHeaders.Add("Origin", "http://");
client.DefaultRequestHeaders.Connection.Add("keep-alive");
client.DefaultRequestHeaders.Add("Connection", "open");
var values = new Dictionary<string, string>
{
...
};
var content = new StringContent(JsonConvert.SerializeObject(values, new KeyValuePairConverter()), Encoding.UTF8, "application/json");
response = client.PostAsync("http://", content).Result;
//Thread.Sleep(milliseconds);
client.DefaultRequestHeaders.Accept.Clear();
//IEnumerable<Cookie> responseCookies = cookieContainer.GetCookies(baseAddress).Cast<Cookie>();
//foreach (Cookie cookie in responseCookies)
//{
// cookieContainer.Add(cookie);
//}
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xhtml+xml"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/webp"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/apng"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
client.DefaultRequestHeaders.Add("Upgrade-Insecure-Requests", "1");
***response = client.GetAsync("http://").Result;***
client.DefaultRequestHeaders.Accept.Clear();
}
When I run this code in dibug mode from my VS 2017 - it runs perfect! after publishing to my iis8 server i get this response:
<HTML>
<head>
<script>
Challenge=;
ChallengeId=;
GenericErrorMessageCookies="Cookies must be enabled in order to view this page.";
</script>
<script>
function test(var1)
{
var var_str=""+Challenge;
var var_arr=var_str.split("");
var LastDig=var_arr.reverse()[0];
var minDig=var_arr.sort()[0];
var subvar1 = (2 * (var_arr[2]))+(var_arr[1]*1);
var subvar2 = (2 * var_arr[2])+var_arr[1];
var my_pow=Math.pow(((var_arr[0]*1)+2),var_arr[1]);
var x=(var1*3+subvar1)*1;
var y=Math.cos(Math.PI*subvar2);
var answer=x*y;
answer-=my_pow*1;
answer+=(minDig*1)-(LastDig*1);
answer=answer+subvar2;
return answer;
}
</script>
<script>
client = null;
if (window.XMLHttpRequest)
{
var client=new XMLHttpRequest();
}
else
{
if (window.ActiveXObject)
{
client = new ActiveXObject('MSXML2.XMLHTTP.3.0');
};
}
if (!((!!client)&&(!!Math.pow)&&(!!Math.cos)&&(!![].sort)&&(!![].reverse)))
{
document.write("Not all needed JavaScript methods are supported.<BR>");
}
else
{
client.onreadystatechange = function()
{
if(client.readyState == 4)
{
var MyCookie=client.getResponseHeader("X-AA-Cookie-Value");
if ((MyCookie == null) || (MyCookie==""))
{
document.write(client.responseText);
return;
}
var cookieName = MyCookie.split('=')[0];
if (document.cookie.indexOf(cookieName)==-1)
{
document.write(GenericErrorMessageCookies);
return;
}
window.location.reload(true);
}
};
y=test(Challenge);
client.open("POST",window.location,true);
client.setRequestHeader('X-AA-Challenge-ID', ChallengeId);
client.setRequestHeader('X-AA-Challenge-Result',y);
client.setRequestHeader('X-AA-Challenge',Challenge);
client.setRequestHeader('Content-Type' , 'text/plain');
client.send();
}
</script>
</head>
<body>
<noscript>JavaScript must be enabled in order to view this page.</noscript>
</body>
</HTML>
What I did so far:
1. I compared my http requests using fiddler from my localhost and production server - they are identical.
2. I checked that my iis website is set to use cookies. I didn't locate the cookie files physically in the server not localhost to compare them - I didn't find them and don't know if its relevant.
Can you think of any lead to the cause / solution?

The same code works using HttpWebRequest but not using HttpRequestMessage

I've created HttpClient that I'm using for sending requests:
public static void Initialize()
{
handler = new HttpClientHandler() { UseCookies = false, AllowAutoRedirect = true };
http = new HttpClient(handler) { BaseAddress = new Uri("http://csgolounge.com/mytrades") };
http.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36");
}
After that I'm creating instance of custom class that stores the cookies string for an account (something like id=xxxxxxxx; tkz=xxxxxxxxxx; token=xxxxxxxxxxx.
That's how I'm sending a post request:
public async Task Bump()
{
//if (Bumpable)
//{
var req = new HttpRequestMessage(HttpMethod.Post, "http://www.csgolounge.com/ajax/bumpTrade.php");
req.Headers.Add("Cookie", cookieString);
req.Headers.Add("X-Requested-With", "XMLHttpRequest");
req.Headers.Add("Referer", "http://csgolounge.com/mytrades"); //Not really sure if this does anything but I've run out of smart ideas long time ago
/*Dictionary<string, string> postData = new Dictionary<string, string>()
{
{"trade", offer_id}
};
var encoded = new FormUrlEncodedContent(postData);
*/
req.Content = new StringContent("&trade="+Offer_id, Encoding.UTF8, "application/x-www-form-urlencoded"); //Desperation.. decided to change the encoded dictionary to StringContent
var res = await SteamAccount.http.SendAsync(req);
var html = await res.Content.ReadAsStringAsync();
//}
}
I don't get what's wrong with this code. It seems correct to me.
Also, when I set AllowAutoRedirect = false it returns 301: Moved Permanently error, while normally it returns 200 with no HTML no matter what I pass as content.
What am I doing wrong?
Edit: Here's the JavaScript function I'm basing my request on:
function bumpTrade(trade) {
$.ajax({
type: "POST",
url: "ajax/bumpTrade.php",
data: "trade=" + trade
});
}
I've worked with more complex AJAX before, but this just doesn't seem to work no matter what I do.
Edit: I've lost my patience and switched to HttpWebRequest instead.
Now the method looks like this:
public async Task BumpLegacy()
{
while (true)
{
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://csgolounge.com/ajax/bumpTrade.php");
var cc = new CookieContainer();
MatchCollection mc = Regex.Matches(Account.CookieString, #"\s?([^=]+)=([^;]+);");
foreach (Match m in mc)
cc.Add(new Cookie(m.Groups[1].Value, m.Groups[2].Value, "/", "csgolounge.com"));
httpWebRequest.CookieContainer = cc;
byte[] bytes = Encoding.ASCII.GetBytes("trade=" + Offer_id);
httpWebRequest.Referer = "http://csgolounge.com/mytrades";
httpWebRequest.Headers.Add("X-Requested-With", "XMLHttpRequest");
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36";
httpWebRequest.ContentLength = (long)bytes.Length;
var g = await httpWebRequest.GetRequestStreamAsync();
await g.WriteAsync(bytes, 0, bytes.Count());
g.Close();
var res = await httpWebRequest.GetResponseAsync();
res.Close();
break;
}
catch
{
}
}
}
Maybe I'm just dumb but for me it doesn't seem all that different. Are there some key differences that can be the cause?
Here is code from one of my working systems that submits a POST request through an HTTPClient.
[Route("resource")]
public async Task<dynamic> CreateResource([FromBody]Resource resource)
{
if (resource == null) return BadRequest();
dynamic response = null;
resource.Topic = GetDataFromSomewhereElse();
var message = new PostMessage(resource).BodyContent;
dynamic postRequest = new
{
Message = message
};
var post = JsonConvert.SerializeObject(postRequest);
HttpContent content = new StringContent(post, Encoding.UTF8, "application/json");
using (var client = new HttpClient())
{
client.Timeout = TimeSpan.FromMinutes(1);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
try
{
client.BaseAddress = #"http://localhost:51145/";
HttpResponseMessage postResponse = await client.PostAsync("Resource", content); //"Resource" is a route exposed on the remote host
string json = await postResponse.Content.ReadAsStringAsync();
if (postResponse.StatusCode == HttpStatusCode.BadRequest) return BadRequest();
if (postResponse.StatusCode == HttpStatusCode.InternalServerError) return InternalServerError();
if (postResponse.StatusCode == HttpStatusCode.NotFound) return NotFound();
return json;
}
catch(Exception ex)
{
return InternalServerError(ex);
}
}
}
[Edit]
The "PostMessage" was modified to remove domain-specific details. Here is how BodyContent is defined inside the real "PostMessage" from my solution, to provide you enough context to understand what that "message" actually is and how it works into the sample.
public string BodyContent
{
get
{
string content = "";
Type type = this.GetType();
Assembly assembly = Assembly.GetExecutingAssembly();
string resource = String.Format("{0}.{1}", type.Namespace, this.EmbeddedResourceName);
Stream stream = assembly.GetManifestResourceStream(resource);
StreamReader reader = new StreamReader(stream);
content = reader.ReadToEnd();
return content;
}
}
...and here is PostRequest (again, with domain-specific details trimmed)
public class PostRequest
{
public string Message { get;set; }
}

Categories