ちょっとはまったので書いておく。
あるサイトにHttpWebRequestでデータをPOSTしようとした。
C#の内部文字コードはUTF-8 送り先はEUC-JP
なので
1.送りたい文字列をEUC-JPに変換
2.1で変換したものをURLエンコード
でいけると思った。
具体的に書くとこんな感じ
//まず文字コードを変換
string data = "日本語の文字列";
byte[] bytes = Encoding.UTF8.GetBytes(data);
string data_euc = Encoding.GetEncoding("EUC-JP").GetString(bytes);
//URLエンコードする
string data_euc_urlencode = HttpUtility.UrlEncode(data_euc));
が 文字化けする
うーんといろいろググった結果 HttpUtility.UrlEncodeの第2引数に文字コードの指定があることが発覚
んじゃあと上のソースの最終行を変更して見るも変わらず。。。
//URLエンコードする
string data_euc_urlencode = HttpUtility.UrlEncode(data_euc,Encoding.GetEncoding("EUC-JP")));
ってかURLEncodeって確か正規表現で1行で書けるような一律置換だから文字コード関係ないはずじゃ・・・
ということで 試しに文字コード変換なしでUrlEncodeの際に文字コードをしてみた。
具体的にはこんな感じ
string data = "日本語の文字列";
//URLエンコードする
string data_euc_urlencode = HttpUtility.UrlEncode(data,Encoding.GetEncoding("EUC-JP")));
めでたく無事解決しました♪
URLエンコードに文字コード変換機能がついてたわけですね。
perlとかじゃありえないありがた迷惑機能 親切機能だ(笑)
とりあえず無事解決してよかった
あるサイトにHttpWebRequestでデータをPOSTしようとした。
C#の内部文字コードはUTF-8 送り先はEUC-JP
なので
1.送りたい文字列をEUC-JPに変換
2.1で変換したものをURLエンコード
でいけると思った。
具体的に書くとこんな感じ
//まず文字コードを変換
string data = "日本語の文字列";
byte[] bytes = Encoding.UTF8.GetBytes(data);
string data_euc = Encoding.GetEncoding("EUC-JP").GetString(bytes);
//URLエンコードする
string data_euc_urlencode = HttpUtility.UrlEncode(data_euc));
が 文字化けする
うーんといろいろググった結果 HttpUtility.UrlEncodeの第2引数に文字コードの指定があることが発覚
んじゃあと上のソースの最終行を変更して見るも変わらず。。。
//URLエンコードする
string data_euc_urlencode = HttpUtility.UrlEncode(data_euc,Encoding.GetEncoding("EUC-JP")));
ってかURLEncodeって確か正規表現で1行で書けるような一律置換だから文字コード関係ないはずじゃ・・・
ということで 試しに文字コード変換なしでUrlEncodeの際に文字コードをしてみた。
具体的にはこんな感じ
string data = "日本語の文字列";
//URLエンコードする
string data_euc_urlencode = HttpUtility.UrlEncode(data,Encoding.GetEncoding("EUC-JP")));
めでたく無事解決しました♪
URLエンコードに文字コード変換機能がついてたわけですね。
perlとかじゃありえない
とりあえず無事解決してよかった
ずいぶん素っ頓狂なことをしていますね。
>//まず文字コードを変換
>string data = "日本語の文字列";
>byte[] bytes = Encoding.UTF8.GetBytes(data);
>string data_euc = Encoding.GetEncoding("EUC-JP").GetString(bytes);
stringの内部表現について、ほんの少し勉強してみた方が良いですよ。
文字コード、で検索すると割と高い順位で表示されるサイトの様なので、こんな赤っ恥は早めに回収した方がいいと思います。
I dont appreciate your template however posts are usually really good well, i will return! Also i cant join your feed! Any ideea why?
you do have a great blog here! do you wish to develop invite posts in my blog?
Aw, this was a really nice post. In idea I wish to put in writing like this additionally – taking time and actual effort to make a very good article… but what can I say… I procrastinate alot and under no circumstances seem to get something done.
Youre so cool! I dont suppose Ive read similar to this before. So nice to get somebody with some authentic ideas on this subject. realy appreciation for starting this up. this web site is something that’s needed over the internet, somebody after a little bit originality. helpful purpose of bringing something totally new towards the web!
What a lovely day for a 2066550! SCK was here
I found your blog using Google and i also must say this really is probably the most informative blogs I've got read in a while. I most certainly will ensure I come back to read your future posts.
The following time I learn a blog, I hope that it doesnt disappoint me as much as this one. I mean, I do know it was my choice to read, but I truly thought youd have something attention-grabbing to say. All I hear is a bunch of whining about something that you might repair if you werent too busy in search of attention.
When I originally commented I clicked the -Notify me when new feedback are added- checkbox and now each time a remark is added I get four emails with the same comment. Is there any method you may remove me from that service? Thanks!
Spot on with this write-up, I truly assume this website needs far more consideration. I’ll most likely be again to learn much more, thanks for that info.
Appreciation for any great post. We are glad We have taken plenty of time to educate yourself this.
This web page is really a stroll-via for all of the information you wished about this and didn’t know who to ask. Glimpse right here, and you’ll undoubtedly discover it.
Zagadnienie zdrowia (a szczególnie żywności ekologicznej) jest mi bardzo bliski. Dzięki za artykuł!
Nice post. I be taught something more difficult on different blogs everyday. It should always be stimulating to read content material from different writers and apply a little bit something from their store. I’d desire to make use of some with the content on my blog whether or not you don’t mind. Natually I’ll give you a link on your net blog. Thanks for sharing.
Kwestia zdrowia (a szczególnie żywności ekologicznej) jest mi bardzo bliski. Dzięki za artykuł!
This web page can be a stroll-through for all the data you needed about this and didn’t know who to ask. Glimpse right here, and you’ll positively discover it.
Hey! I simply would like to give a huge thumbs up for the good data you could have right here on this post. I will likely be coming back to your blog for extra soon.
This web page is mostly a stroll-via for all of the data you wished about this and didn’t know who to ask. Glimpse here, and also you’ll positively uncover it.
An impressive share, I just given this onto a colleague who was doing somewhat evaluation on this. And he the truth is purchased me breakfast as a result of I discovered it for him.. smile. So let me reword that: Thnx for the deal with! However yeah Thnkx for spending the time to discuss this, I feel strongly about it and love reading more on this topic. If potential, as you become expertise, would you thoughts updating your weblog with extra details? It's highly useful for me. Large thumb up for this blog publish!
you've gotten an important blog here! would you like to make some invite posts on my blog?
It’s arduous to search out knowledgeable folks on this subject, but you sound like you understand what you’re speaking about! Thanks
Today, while I was at work, my cousin stole my iphone and tested to see if it can survive a twenty five foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views. I know this is entirely off topic but I had to share it with someone!
There is noticeably a bundle to find out about this. I assume you made certain good factors in features also.
Hello, I feel I can see the matter through your eyes as I discriminate the same way & couldn’t help but drop a line to thank you for your time. On a side note I fell you would love this home building website I stumbled upon right before I found yours. There is loads of good views similar to your site. They have everything for if your thinking of building a custom estate in Perth, WA. Just trying to help
It’s laborious to seek out knowledgeable folks on this topic, but you sound like you realize what you’re speaking about! Thanks
Today, while I was at work, my cousin stole my iphone and tested to see if it can survive a twenty five foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views. I know this is entirely off topic but I had to share it with someone!
Thank you a lot for sharing this with all of us you really realize what you're talking approximately! Bookmarked. Kindly also consult with my web site =). We can have a hyperlink exchange agreement between us!
Youre so cool! I dont suppose Ive read something like this before. So good to search out somebody with some unique ideas on this subject. realy thank you for starting this up. this website is one thing that's wanted on the net, someone with slightly originality. helpful job for bringing one thing new to the internet!
Nice! Just wanted to respond. I thoroughly loved your post. Keep up the great work.
Youre so cool! I dont suppose Ive read something like this before. So nice to seek out somebody with some unique thoughts on this subject. realy thank you for beginning this up. this web site is something that's wanted on the web, someone with slightly originality. helpful job for bringing one thing new to the web!
Can I simply say what a reduction to seek out somebody who truly is aware of what theyre speaking about on the internet. You positively know how you can bring an issue to mild and make it important. More people must learn this and understand this facet of the story. I cant believe youre not more common since you positively have the gift.
There are some fascinating time limits on this article however I don’t know if I see all of them middle to heart. There may be some validity however I will take maintain opinion until I look into it further. Good article , thanks and we wish more! Added to FeedBurner as properly
I’d need to examine with you here. Which is not one thing I often do! I get pleasure from reading a put up that will make people think. Additionally, thanks for allowing me to remark!