Problem of URL Submission to Bing.

melon NG 296 Reputation points
2022-03-15T07:25:43.577+00:00

I am about to submit URL to Bing for SEO every day.

Here is the API:
https://www.bing.com/webmasters/url-submission-api#APIs

I achieve this by Json Request-Response.

Here are my codes:

private async Task BingSubmission(string Website, List<string>SubmissionList)
{
BingSubmissionModel BingSubmission = new BingPushSubmission() { siteUrl = Website, urlList = SubmissionList };
var options = new JsonSerializerOptions();
options.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(UnicodeRanges.All);
var JSONContent = JsonSerializer.Serialize(BingSubmission, typeof(BingSubmissionModel), options);
string Content = "";
try
{
var request = new HttpRequestMessage(HttpMethod.Post, "ssl.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey=abc");
request.Content = new StringContent(JSONContent, Encoding.UTF8, "application/json");
var client = HttpClientFactory.CreateClient();
client.DefaultRequestHeaders.Host = "ssl.bing.com";
client.DefaultRequestHeaders.Accept.Add((new MediaTypeWithQualityHeaderValue("application/json; charset=utf-8")));
client.Timeout = TimeSpan.FromSeconds(5);
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
Content = await response.Content.ReadAsStringAsync();
}
else
{
Content = "failed";
}
}
catch (Exception ex)
{

        }
    }

private record BingSubmissionModel
{
public string siteUrl { get; init; }
public List<string> urlList { get; init; }
}

However, after the codes ran. It throws this error as below:
System.FormatException: The format of value 'application/json; charset=utf-8' is invalid.
at System.Net.Http.Headers.MediaTypeHeaderValue.CheckMediaTypeFormat(String mediaType, String parameterName)
at System.Net.Http.Headers.MediaTypeHeaderValue..ctor(String mediaType)
at System.Net.Http.Headers.MediaTypeWithQualityHeaderValue..ctor(String mediaType)

What's wrong with my code? Thank you.

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,851 Reputation points Volunteer Moderator
    2022-03-15T17:31:29.467+00:00

    as the error states:

    "application/json; charset=utf-8"

    is not a valid accept header value. you do not specify the charset with the accept header. you can set the priority if multiple types

    Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, /;q=0.8

    as you are only accepting one type, try:

    "application/json"

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.